• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner Class - Initialization Sequence

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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“?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try the following code (warning: I didn't tested it...):

This should give you
false
true
Did that help?
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Think of how dumb the average person is. Mathematically, half of them are EVEN DUMBER. Smart tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic