• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Nested class

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class OuterTest {

String id ;

OuterTest ( ) {
this.id = " Default " ;
}

OuterTest ( String id ) {
this.id = id ;
}

class InnerTest extends OuterTest {

void doSomething ( ) {
System . out . println ( OuterTest.this.id ) ;

}
};

public static void main ( String args [ ] ) {
OuterTest outer = new OuterTest ( " STP " ) ;
InnerTest inner = outer . new InnerTest ( ) ;
inner.doSomething ( ) ;
}
};


Hi,
The above code prints output as " STP ". When the inner class instance is created using outer class, the inner class constructor calls super class constructor which basically executes this.id = "default". I thought it should print "default" instead of " STP ". Please clarify.
 
Sheriff
Posts: 22650
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you create an outer class using "STP". This will be the id of the outer class.
Then, you use that outer class to create an inner class. This will have id "Default" as you expected.

However, when you call doSomething, it prints OuterTest.this.id - the id of the outer class, which you set at "STP". If you would print id as well then you'd get "Default".
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ashok,
The O/P of your program is correct: " STP ",
That is because the call:
OuterTest outer = new OuterTest ( " STP " ) ;
initializes the variable OuterTest.id to STP, by the non-default constructor call.
Now, the call
InnerTest inner = outer . new InnerTest ( ) ;
defines an instance of inner class, which is having an instance of outer class associated with it.
So, when you call the method
inner.doSomething( );
then it prints: " STP ", the current value of instance variable of the class OuterTest.

Should you've coded your code something like:
OuterTest outer = new OuterTest();
InnerTest inner = outer.new InnerTest();
inner.doSomething();

will print "Default" as you may want.

Hope this will help.
Regards,
 
We find this kind of rampant individuality very disturbing. But not this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic