• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static variables?  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey people I know why non static variables cannot be called from a static method and why non static methods can't be called from static methods like main because how can they call something that might not/does not exist but how come you are allowed to call static variables from non static methods?

Thanks
 
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
You can't call instance (nonstatic) methods from a static method because the called method needs an instance of the class (an object) to work. But calling a static method from a instance method is fine since the called method doesn't require any instance.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
…and because the static members do not require an instance, they are guaranteed to exist before the instance is created to call an instance method on.
 
nettech india
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static variables are those variables whose life time remains equal to the life time of the program. Any local or global variable can be made static depending upon what the logic expects out of that variable.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nettech india wrote: . . . Any local or global variable can be made static depending upon what the logic expects out of that variable.
That sounds to me like poor, non‑object‑oriented thinking. There should be no such thing as a global variable. Also it is probably a bad idea to have any static private variables.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!