• 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

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: 22783
131
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,
 
reply
    Bookmark Topic Watch Topic
  • New Topic