This will compile properly and will print STP. (tried myself.)
1) The main method creates an instance of OuterTest class. The constructor OuterTest(String) runs. The "id" instance variable gets the value "STP".
2) The main method creates an instance of InnerTest class.
3) The doSomething() method of inner object runs. Inside doSomething(), the id instance variable's value of the for the outer instance is printed.
The only confusing syntax could be "OuterTest.this.id." The outer object's 'this' reference can be accessed from the inner class using the outerclassname.this syntax. [ July 26, 2006: Message edited by: Neelesh Bodas ]
See this also 'InnerTest extends OuterTest also When we instantiate InnerTest class its super class also get instantiate & id became "Default" but it is refering to "outer . new InnerTest();" which is earlier instantiate to "STP"
you are probably getting confused between the enclosing object of the OuterTest class and the parent object of the OuterTest class.
this.id returns the value of the id instance variable of the current object super.id returns the value of the id instance varaible of the parent object OuterTest.this.id returns the value of id instance variable of the enclosing Object.
Observe that there are two different OuterTest objects in picture - one is an instance of enclosing class and another is an instance of the parent class. [ July 26, 2006: Message edited by: Neelesh Bodas ]
In the above example, an instance of class C will be associated with an instance of enclosing class B. At the same time, since C is a subclass of A, there will be a parent instance of A along with (or 'within' ) every instance of C.
This is the difference between "enclosing" class and "parent" class.
In the given example, it so happens that A and C are is the same class, OuterTest. Ofcourse, this doesn't mean that there is only one instance. There will certainly be two instances, one in the role of parent object and other in the role of enclosing object.
To answer the second part, since the superclass constructor runs before the subclass constructor, hence 'id" in the superclass-instance has already got the value "Default" before the value gets printed in the subclass's constructor. [ July 27, 2006: Message edited by: Neelesh Bodas ]