• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static variable

 
megha kanth
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
megha kanth
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..

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

we can't use static keyword iniside the method.
 
Sanyev Babu
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
megha kanth
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thx everyone for clearing mine basics.



megha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic