• 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

Understanding Inner Class Output

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 "
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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."

 
Ethan Bosco
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you marc..
 
If you were a tree, what sort of tree would you be? This tiny ad is a poop beast.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic