• Post Reply Bookmark Topic Watch Topic
  • New Topic

why cant you directly reference Instance variables in main, and have to use a method?  RSS feed

 
martin andreas
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I noticed you cannot reference an Instance variable from main directly, but only through an object.

However, if that same Instance variable is being referenced from a a method directly, it's OK

example:

 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method "main" is static; it doesn't have access to any instance.  So what instance would be used to print the instance variable?
 
Julius Adek
Greenhorn
Posts: 26
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because locationCell is only locally accessible to an instance of TryOut object. So, it can not be called without the instance of this object. As you can see that before you called assign() it was called through an instance object of TryOut called big. However, if this locationCell has been created as static variable; you would have been able to call locationCell within any static or reference method especially the main method. All static members of a class are always globally accessible.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
martin andreas wrote:. . . for what reason can't you use instance variables straight from main? . . .
To expand on what Knute said: Remember that there is one instance of each static member of a class, irrespective of how many instances there are. So there is one main method, whether there are 1, 2, 3, 1,000,000,000 instances . . . or even no instances at all. If you are in the main method, how are you to know which copy of an instance member you want? What if there aren't any instances yet, and there aren't any instance fields in existence yet? There is no way to verify that sort of thing, so it won't compile. You get what I think is a confusing error message about things non‑static, but I think the correct solution is to move as much of that code as possible out of the main method.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Julius Adek wrote:. . . However, if this locationCell has been created as static variable; you would have been able to call locationCell within any static or reference method especially the main method.
Yes, but the idea of object‑oriented programming is to have as much as possible of the code relating to objects. I don't like adding the keyword static if it is possible to change things from static to “instance”? Do you mean, “instance method,” by, “reference method”.
All static members of a class are always globally accessible.
You mean that static members have an existence as soon as the class is loaded.
 
Julius Adek
Greenhorn
Posts: 26
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They exit as soon as the class is loaded. static members of class always exit even without creation of any object of that specific class, you can directly access a static member through the name of the class but for you to access any instance member there must be an object attach to it. For example, if assign() method was created as "static void assign(){ }"; then you should be able to call assign() within the main() method without calling it through "big" object although you can still call it through it. However, static variable has only one original copy in all the objects of a class; if you create "static int x = 5;" in a class called "Rectangle". Then , you now create 3 objects from Rectangle class called "rec1", "rec2", and "rect3" respectively. The value "x" in all these 3 objects would be the same that is, if you change "x = 20;" from "rec1". This new value will be available in "rec1", "rec2" and "rec3".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!