wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Understanding Inner Class Output 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 "Understanding Inner Class Output" Watch "Understanding Inner Class Output" New topic
Author

Understanding Inner Class Output

Ethan Bosco
Greenhorn

Joined: Nov 01, 2004
Posts: 15
I am unable to understand how Outertest.this.id has a value of "STP" instaed of " efault"



Output :- Compiles fine & prints " STP " to the console
Nina Binde
Ranch Hand

Joined: Sep 24, 2004
Posts: 85
Since you are calling the construtor of OuterTest ( String id ) in your main method, it initialises the id variable with "STP". If you replace this:
outer = new OuterTest ( " STP " ) ;
with
outer = new OuterTest () ;

You will see the output of " Default "
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

This example is a little confusing because InnerTest is an inner class of OuterTest, but it also extends OuterTest.

Because InnerTest inherits from OuterTest, the default constructor of OuterTest is called in the process of creating an InnerTest object (as demonstrated by the println statements below).

However, inner class objects must be associated with a particular instance of the enclosing class. And in this case, you're specifically associating it with the instance called "outer," which has its variable id set to "STP."



"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ethan Bosco
Greenhorn

Joined: Nov 01, 2004
Posts: 15
Thanks Nina for the response but I am still unable to understand you explanation. I have modified the code a bit below, when the line //LINE X is executed the default construcotr of OuterTest is called which should set
the String variable id to "Default", the output now is

After OuterTest construction
Default constrcutor is called
After Inner Construction
STP




Can you help me out
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Ethan,
I believe Outer.this refers to the "this" of Outer Class and hence it refers to the id of the Outer Class.

Every class is associated with "this" reference
Outer object would like this in memory
------- * this Outer Class
| id = |
| "STP" |
-------

Inner object would be like this in memory
----------- * this Inner Class
| id = |
| "default" |
-----------
So when the call is Outer.this.id it will call refer to the Outer class "this" and then call the id of the Outer class.

If you change the line OuterTest.this.id to InnerTest.this.id it will print "default"

[BOLD]
void doSomething ( ) {
System . out . println ( InnerTest . this . id) ;
}
[/BOLD]


Hope this helps you.

[ November 01, 2004: Message edited by: Jay Pawar ]
[ November 01, 2004: Message edited by: Jay Pawar ]

Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
In method doSomething, try changin to
System.out.println ( InnerTest . this . id ) ;

By changing it to InnerTest, you will get "Default".
What i think, is when a variable is considered by the "Type of variable that refers" (In this case type of reference variable is InnerTest.) So, it is pointing to "Default.

In you case, "Type of variable is "OuterTest" (Value of variable id is set to "STP")
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Following example may help you :
class tst
{
public static void main(String ara[])
{
c C = new c();
}
}

class a
{
int i = 10;
void amethod()
{
System.out.println("amethod Executed!");
}
}

class b extends a
{
int i = 20;
void amethod()
{
System.out.println("bmethod Executed!");
}
}

class c
{
c()
{
b B = new b();
System.out.println("bTypeVariable="+B.i);
B.amethod();

a A = new b();
System.out.println("aTypeVariable="+A.i);
A.amethod();
}
}


"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

In this odd situation of extending the enclosing class, we can demonstrate that the inner class has access to both String id's: The one it inherits (valued with the no-args constructor of the parent class), and the one in the instance of the enclosing class it's tied to (valued with the String constructor).

But can someone talk me through the reference: OuterTest.this.id?

"OuterTest" is a class (not an instance), but "this" is a reference to the calling instance. So what exactly does "OuterTest.this" mean?


[ November 02, 2004: Message edited by: marc weber ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
This is an annotated version of the original code:

And this is what it prints:


Maybe now you can better see what's going on.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Arshad Ahamed
Greenhorn

Joined: Nov 03, 2004
Posts: 5
Please Explain the line
InnerTest inner = outer.new InnerTest ( );

how this line, initalize outerTest() constructor ?, as it referred as "outer.new InnterTest()"

Thanks in advance.


Be YourSelf !!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Arshad Ahamed:
Please Explain the line InnerTest inner = outer.new InnerTest(); how this line initalize outerTest() constructor...

Although an instance of OuterTest is already referenced by "outer," a constructor for OuterTest is called again because InnerTest also extends OuterTest. That is, it's not just an inner class -- it's also a subclass.

Barry: Thanks for your annotated code. I'm still interpreting this.
[ November 03, 2004: Message edited by: marc weber ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Barry:

Within Outer.Inner, I can see that Outer.this returns a reference to the enclosing instance of Outer. However, I'm confused on two points:

First, my understanding is that "this" is a reference within a non-static method to the instance that's calling the method.

So if I create an instance of Outer.Inner, then use that to call an instance method in Outer.Inner that returns Outer.this, then why isn't the calling instance (this) actually Outer.Inner?

Second, since Outer is a class (not an instance), then how are we able to reference Outer.this unless "this" is a static member of Outer?

Does "this" have special meaning within the context of inner classes?
Arshad Ahamed
Greenhorn

Joined: Nov 03, 2004
Posts: 5
Thank you marc..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Understanding Inner Class Output