wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt" Watch "Doubt" New topic
Author

Doubt

sravanthi pulukuri
Ranch Hand

Joined: Mar 15, 2007
Posts: 125
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
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi,

I would also like to know what is happening there!



Thanks and Regards,
cmbhatt


cmbhatt
sravanthi pulukuri
Ranch Hand

Joined: Mar 15, 2007
Posts: 125
something is going on in the inside the method of inner class
there i was struck up
Please help me out
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Mar 15, 2007
Posts: 125
soory for the delayed reply the it should print STP na
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt
 
Similar Threads
Inner Classes
Nested class
Inner class constructor
Understanding Inner Class Output
Inner class