• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Doubt

 
Ranch Hand
Posts: 125
  • 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 ( ) ;
}
};
can you explain me why it is giving output as STP
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I would also like to know what is happening there!



Thanks and Regards,
cmbhatt
 
sravanthi pulukuri
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
something is going on in the inside the method of inner class
there i was struck up
Please help me out
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's pretty straightforward.

An OuterTest object named outer is created with the parameter " STP".

This object is then used to create an instance of it's inner class, InnerTest.

InnerTest extends OuterTest so it inherits the field id. But that doesn't really matter.

The important thing is that in the method doSomething, you are using the syntax OuterTest.this.

An inner class can access it's outer class's this with that syntax.

So you are printing out the field id associated with the instance of OuterClass that was used to create the instance of the inner class.
[ March 30, 2007: Message edited by: Keith Lynn ]
 
sravanthi pulukuri
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
soory for the delayed reply the it should print STP na
 
Paper jam tastes about as you would expect. Try some on this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic