• Post Reply Bookmark Topic Watch Topic
  • New Topic

Non staic variable  RSS feed

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,

How to call a non static instance variable in static method (shiva()).
I don't mean to call from static main method.Help me please
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to call a non static instance variable in static method

Like this: Cup your hands around your mouth and yell, "Yo, variable!".



Ok, really, you can't do this if I understand your question correctly.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LOL Gregg
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greig Bolinger has already given you the short answer about calling a non-static variable from a static context.

Now turn away before I give the long answer . . .

You have a class, and it has static variables. You can have as many instances of a class as you can fit into the memory available on your computer, but each of them has a (or one of each)static variable. It is always the same variable. There can be 1000000 instances, but there is only one copy of each static variable. Every instance of the class has access to the same static variable. It only appears once in memory. And whenever any instance gets access to it, that static variable is always the same. [If you ever do C++ or C# programming, you will find exactly the same rules about static variables there.] Similarly there is only one copy of each static method per class.
If there are NO instances of the class at all (I am indebted to Deitel P, Deitel H A, Java-How to Program 6/e, Upper Saddle River NJ: Prentice-Hall (2005), page 384 for this bit of info), you still can call a static method. The commonest example has public before the static, and void main(String[] args) after.

Now look at it the other way round.
You can call a static method, or a static variable. There is only one of each of them. Never two. Always one. But when you call a static member, there might be 1000000 instances of the class to choose from. Which do you choose? Whose instance variable might you access when there are 1000000 different ones to choose from?
When you call a static member (particularly the main method), there might be NO instances going, so there are no instance variables. How can you call an instance variable or method when there aren't any instance to choose from?

So, the compiler won't let you call an instance variable from a static method.
It won't let you call an instance method from a static method. Only it calls it non-static.
And you can't call the keyword this from a static method.

There is a list of compiler messages on this website, but it only gives brief explanations.

CR
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's an instance variable, then you need an instance of the enclosing class in order to access it. You cannot access an instance variable directly from a static context. But you could access the variable from a static method if that method had a reference to an instance (either passed into the method or created within the method).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!