473,473 Members | 2,309 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

non-static variable error ( simple )

2 New Member
The problem in the follow code occurs when trying to instantiate an array object.

Expand|Select|Wrap|Line Numbers
  1. public class Main {
  2.  
  3.     /**
  4.      * @param args the command line arguments
  5.      */
  6.     public static void main(String[] args) {
  7.  
  8.         Room[] rooms;
  9.         rooms = new Room[5];
  10.  
  11.         rooms[0] = new Room( 3, 7, true);
  12. /* non-static variable this cannot be referenced from a static context */
  13.  
  14.         for(int i = 1; i < 5; i++)
  15.         {
  16.             room[i] = new Room( 5, 5, true);
  17. /* non-static variable this cannot be referenced from a static context */
  18.         }
  19.         for(int i = 0; i < 5; i++)
  20.         {
  21.             room[i].printRoom();
  22. /* non-static variable this cannot be referenced from a static context */
  23.         }
  24.     }
  25.  
  26.     /* ****************************************** */
  27.  
  28.     class Room
  29.     {
  30.         int beds = 2; // 2 as defaukt number of beds
  31.         int guests = 0; // 0 as default number of guests
  32.         boolean smoking = false; // false as default for smoking
  33.  
  34.         Room(int beds, int guestss, boolean smoking)
  35.                 /*
  36.                  * A contructor for the Room class
  37.                  */
  38.         {
  39.             this.beds = beds;
  40.             guests = guestss;
  41.             this.smoking = smoking;
  42.         }
  43.  
  44.         public void printRoom()
  45.         {
  46.             System.out.println("Beds: " + beds + ". Guests: " +
  47.                     guests + ". Smoking: " + smoking);
  48.         }
  49.     }
  50.  
  51. }
/* non-static variable this cannot be referenced from a static context */
Why does this happen? In other words, could someone explain in brief what's happening - And how to fix it.

Thanks in advance,

Jon
Mar 14 '09 #1
1 1993
JosAH
11,448 Recognized Expert MVP
Your array is named 'rooms', not 'room', so you have rooms rooms[0], rooms[1] ... etc.

kind regards,

Jos
Mar 15 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
17
by: cheeser | last post by:
Hello all, Please see the question in the code below... Thanks! Dave #include <iostream>
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
1
by: Markus Ernst | last post by:
Hi I wrote a function that "normalizes" strings for use in URLs in a UTF-8 encoded content administration application. After having removed the accents from latin characters I try to remove all...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
4
by: bwmiller16 | last post by:
Guys - I'm doing a database consistency check for a client and I find that they're building unique indexes for performance/query reasons where they could be using non-unique indexes. Note...
0
by: Christopher Attard | last post by:
Hi, I need to create a dialog like the 'Add Counters' dialog box in perfmon. I'm using the System.Diagnostics namespace class in .NET and I've managed to do it. The problem arises when I'm...
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.