wood burning stoves 2.0*
The moose likes Java in General and the fly likes Inner Class - Initialization Sequence 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 » Java » Java in General
Bookmark "Inner Class - Initialization Sequence" Watch "Inner Class - Initialization Sequence" New topic
Author

Inner Class - Initialization Sequence

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Initialization Sequence of this code:
- When outer is created in line 1 the constructor OuterTest(String id) is called
and „id“ of OuterTest is initialized to „STP“ and „Default Constructor
is printed to console.
- Then the instance „inner“ of the inner class is created. As inner class extends
The outer class the Default-Constructor of OuterTest is called and „NonDefault
Constructor“ is printed.
MY QUESTION is: Why didn’t get „id“ of OuterTest the value „Default“ which is
Set by the default constructor of „OuterTest“?
Instead the „Outer.Test.this.id“ keeps ist old value „STP“ from the creation of the
OuterTest-instance „outer“.


C:\Java\EigeneJavaProgramme>java OuterTest
NonDefault-Constructor
Default-Constructor
InnerTest STP
InnerTest Default
=> Question: Why is „OuterTest.this.id“ still „STP“ and not „Default“?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Try the following code (warning: I didn't tested it...):

This should give you
false
true
Did that help?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Pete Lyons
Ranch Hand

Joined: Aug 18, 2002
Posts: 109
Let's see if I can explain what's going on. The key thing to focus on is that Inner is BOTH an inner class and a subclass. Because it's an inner class, it is automatically associated with an instance of it's outer class. Because it is a subclass, it inherits all it's superclass's properties. So when you construct the OuterTest in line 1, you get 1 instance with id set to " STP ". Now in line 2, you construct a new InnerTest via this new OuterTest, but remember that this new instance is a subclass of OuterTest, so it has it's OWN String id property, and that's what get's set to " Default ", leaving the outer instance's id unchanged. So from within the InnerTest class, OuterTest.this.id refers to the id property of the instance of Outertest that the current instance of InnerTest is implicitly associated with, and just id refers to the id property that the current instance of InnerTest inherits from the OuterTest class. Does that clarify things at all? This stuff can get very confusing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner Class - Initialization Sequence