aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confusion with static variable.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confusion with static variable...." Watch "confusion with static variable...." New topic
Author

confusion with static variable....

babai bhaumik
Greenhorn

Joined: May 12, 2007
Posts: 18
// 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.


Be bold.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Gunjan Kumar
Ranch Hand

Joined: Mar 26, 2007
Posts: 74
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
}


Gunjan Kumar
SCJP 1.5
babai bhaumik
Greenhorn

Joined: May 12, 2007
Posts: 18
Hi,
Thanks a lot, to both of you. It has cleared my doubt.
Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confusion with static variable....