Reputation: 171
I got a question about how to use static, I saw a sample:
public class Exe {
static int i = 47;
public void call() {
for (i = 0; i < 3; i++) {
if (i == 2) {
System.out.println("\n");
}
}
}
public Exe() {
}
public static void main(String[] args) {
Exe t1 = new Exe();
Exe t2 = new Exe();
t2.i = 60;
System.out.println(t1.i);
t1.call();
System.out.println(t2.i);
t2.call();
}
}
When I tried to run it, it printed 60 3, I am wondering why t2.i here is 3, I do not know where the 3 comes from, also, the both results of t1.call() and t2.call() were not printed, please advise, thank you!
Upvotes: 0
Views: 171
Reputation: 122026
for(i=0; i<3;i++){
if(i==2){
System.out.println("\n");
}
}
Your static variable Assigned/incremented here.
Not the i (which you are assuming, it's different) in for loop.
To clear the clouds, Just take another variable called j
and do the looping.
for (int j = 0; j < 3; j++) {
if (j == 2) {
System.out.println("\n");
}
}
Upvotes: 2
Reputation: 2315
Static members doesn't reflect the object state!
All objects of a class will have the same copy(mean to say same value) of the static variables. If one object changes the value of a static variable, since the static variable holds the same value across all the objects, the static variable value will be updated across all the objects of that class. This rule reflects your result.
See the below example:
public class StaticTest {
static int staticA = 10;
int intA;
public StaticTest(int intA){
this.intA = intA;
}
public static void main(String[] args) {
StaticTest test1 = new StaticTest(10);
System.out.println(test1.intA);
System.out.println(test1.staticA);
StaticTest test2 = new StaticTest(20);
System.out.println(test2.intA);
System.out.println(test2.staticA);
}
}
Below is the outpput:
10 10 20 10
Upvotes: 0
Reputation: 4194
Your static variable is the same across all objects, this is an effect of static
. This means that when you call t2.i = 60;
both t1.i and t2.i are set equal to 60. Then when you call t1.call();
you again change both objects. As others have explained your for loop is setting the variable i = 3.
It's because it's a static variable that the assignment affects both objects
Upvotes: 0
Reputation: 62072
static
means that every instance of the class has access to the same, single instance of the variable.
When you create t1
, t1.i
is initialized to 47
. Then you create t2
. t2.i
and t1.i
are the same variable, so whether you do t1.i = 60
or t2.i = 60
, they're BOTH equal to 60
.
So before you do t1.call();
or t2.call()
, the first thing you do is print out t1.i
, which is 60
, as per the line t1.i = 60;
.
Then you run t1.call()
which runs through the for
loop. The for
loop exits when i
can't pass the test i < 3
, and since i
is an integer, this happens as soon as i
is incremented to 3
.
After you've run t1.call()
, i
is now equal to 3
. This means both t1.i
and t2.i
since static
means there's only one copy of i
across all instances of the Exe
class here. So you print out t2.i
and it is equal to 3
, as it should be.
Hope this helps.
Upvotes: 1
Reputation: 69035
for (i = 0; i < 3; i++)
In this statement instead of treating i as a local variable it is taken to be class variable which you defined. As for class variables only one copy is maintained for all the instance after the for loop finished(i=3) value of you class variable i remains to be 3.
Upvotes: 0
Reputation: 77234
The 3
comes from your for
loop, which is reusing the same static int i
that you're manually setting to 60
before you call call()
. The results of the println
probably are being printed, but they're just blank lines.
Upvotes: 1