Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confusion with static variable....

 
babai bhaumik
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// test results challenged....

class Test
{
static int s;
public static void main(String [] args)
{
Test p = new Test();
p.start();
System.out.println(s);
}

void start()
{
int x = 7;
twice(x);
System.out.print(x + " ");
}
void twice(int x)
{
x = x*2;
s = x;
}
}


In the above mentioned case, the code will compile without error . Where as my understanding is that we cannot access static variable from a nonstatic method, provided we are not instantiating the class in to that method.
Clearing the doubts on this will be of great help.
Thanks in advance.
Bhaumik.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you're memorized a rule, but memorized it incorrectly. Instead, let's try to get you to understand.

Instance variables are part of the objects of a class. If class A has a member x, then if there are 100 A's, then there are 100 x's, one inside each A object. It doesn't make any sense to try to refer to "x" without specifying which instance of "A" we're talking about -- it's like asking for the age without specifying which person's age we're talking about. It has no meaning. We have to say "the x of this A".

Since static methods in class A aren't "attached" to an individual object, the body of a static method can't just say "x" -- it must have an actual instance of the class A so that it can say "the x in this A."

So that's the rule: code in static methods can't refer to instance ("non-static") variables without having an object reference to use; that's pretty much the opposite of what you said here.

I'm going to move this to SCJP, as it's hardly an advanced Java question.
 
Gunjan Kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where as my understanding is that we cannot access static variable from a nonstatic method, provided we are not instantiating the class in to that method.


We can access static variable from non static method without instantiating that class
Example:

int x;
static int y;
public static void main(String[] args) {
// TODO code application logic here
//System.out.println(x);// cant access instance variable, without making object
statc s = new statc();
System.out.println(s.x);
s.nonStatic();
}
public void nonStatic()
{
System.out.println(y);//static variable can be access from nonstatic method
}
 
babai bhaumik
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks a lot, to both of you. It has cleared my doubt.
Thanks again.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic