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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static variable" Watch "static variable" New topic
Author

static variable

megha kanth
Ranch Hand

Joined: Mar 10, 2004
Posts: 44
hi,

Pls tell me if I am correct. There is only one copy of static for all instances. Right?

Can we reintialize a static variable.Suppose there is a methodA-

int methodA(){
static int i=0;
i++;
}

And now suppose there is a classA which access this methodA.
ClassA{
ClassA a=new ClassA();
a.methodA();
System.out.println(i);
a.methodA();
System.out.println(i);
}

first time when methodA is accesssed the i value changes to 1 then when second time methodA is accessed then Will i be again intialized to 0 or will it be considered 1?

tks
megha


Megha Kanth<br />SCJP 1.4
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
A static variable belongs to the "class," not to any given instance of that class. When the class is loaded, any static variables of that class are initialized. However, unless that static variable is declared as final, its value can be changed whenever you like.

Note that static variables are not "reinitialized" when a new instance of the class is created. Static variables are initialized just once, when the class the variable is defined in is loaded.


SCJP Tipline, etc.
megha kanth
Ranch Hand

Joined: Mar 10, 2004
Posts: 44
thx for the reply.
this means that the output of above program will be 2. Every time method is accessed it will skip the statement "static int i=0;"

Correct me if I am wrong.

megha
Beny Na
Ranch Hand

Joined: May 26, 2004
Posts: 159
Hi..

int methodA(){
static int i=0;
i++;
}

we can't use static keyword iniside the method.
Sanyev Babu
Ranch Hand

Joined: Dec 18, 2003
Posts: 132
First and the for most as Beny pointed out you cannot declare a local variable as static.
All variable declaration that happen inside a method are local. The only modifier acceptable inside a method is final. So static is illegal there.
So the method
int methodA(){
static int i=0;
i++;
}
Will cause a compile time error.
The only place where you can write static int i = 0 is outside any method. That is as a static class variable and this is evaluated only once.
Hope it is clear.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

if you had code like this:



note that a and b are not declared inside any method. i'm pretty sure both a and b are created and initialized when the class is loaded by the JVM. That means that the FIRST time your program creates a myClass. however, EVERY time you create a myClass, a c variable is made for that instance.

in other words, at the first "new myClass()", the JVM says "boy, i don't know what a myClass is - i better figure that out. Let me read the definition of the class... OH, i need to make two static variables, a and b, and mark b as 'never EVER change this'. ok, got that done. now, let me make the actual instance...blah blah blah".

the second time you get to a "new myClass()", the JVM says "i already know what a myClass is, so i'll just make one with a c".

having said all that, if you have a method that changes a, that change is reflected in every instance that has been, or will be created. a does not get re-initialized when a new instance is created.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
megha kanth
Ranch Hand

Joined: Mar 10, 2004
Posts: 44
thx everyone for clearing mine basics.



megha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static variable