Thanks Suma to show us how the compiles works.
I would like to point that using "this" to access static variables is not the better way to do that. It is more difficult when we have more than one instance that uses static class variables. In this case it could not be clear, during programming, where the variables is related to class or to instance.
In the code segment bellow, both "println" give the same result but the second one is more clear and, im my opinion, there is no good reason to use the first one.
public class TestThis{
static int j = 20;
public static void main(String args[]){
TestThis
test = new TestThis();
test.amethod();
TestThis test2 = new TestThis();
test2.amethod();
}
public void amethod(){
TestThis.j++ ;
System.out.println("Value of static variable j is " + this.j);
System.out.println("Value of static variable j is " + TestThis.j);
}
}
And its prints:
Value of static variable j is 21
Value of static variable j is 21
Value of static variable j is 22
Value of static variable j is 22
We know that j is never relatad to instance, but it could be wrongly assumed.
But I agree that Suma�s code is important to understand
java compiler rules.
Adrian