• 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

Inner class

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,

can you explain the following code.Iam not able to get what is happening here?How constructors are working here?
Please Help me.


Question :
Will the following code compile ? If yes , what is the output ?

[ July 26, 2006: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain this code. I am not able to understand OuterTest . this

What does it mean? Since doSomething() method is called from inner instance, so this refers to inner instance. Calling this from class name OuterTest looks like inner instance is static. But its not?

Can anyone explain this?

Thanks

Naseem
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This will compile properly and will print STP. (tried myself.)

1) The main method creates an instance of OuterTest class. The constructor OuterTest(String) runs. The "id" instance variable gets the value "STP".

2) The main method creates an instance of InnerTest class.

3) The doSomething() method of inner object runs. Inside doSomething(), the id instance variable's value of the for the outer instance is printed.

The only confusing syntax could be "OuterTest.this.id." The outer object's 'this' reference can be accessed from the inner class using the outerclassname.this syntax.
[ July 26, 2006: Message edited by: Neelesh Bodas ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"OuterTest.this" in the context of the doSomething() method in InnerTest is a reference to the containing object outer.


In other words: the containing object's this reference.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this also 'InnerTest extends OuterTest also
When we instantiate InnerTest class its super class also get instantiate & id became "Default" but it is refering to "outer . new InnerTest();" which is earlier instantiate to "STP"

Seems as per its static method .iam too confuse.

 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are probably getting confused between the enclosing object of the OuterTest class and the parent object of the OuterTest class.

this.id returns the value of the id instance variable of the current object
super.id returns the value of the id instance varaible of the parent object
OuterTest.this.id returns the value of id instance variable of the enclosing Object.

Observe that there are two different OuterTest objects in picture - one is an instance of enclosing class and another is an instance of the parent class.
[ July 26, 2006: Message edited by: Neelesh Bodas ]
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very good, Neelesh!!!
 
Sireesha Mullapudi
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You All
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neeleesh, what is the difference between parent object and enclosing object. Shouldn't super.id print 'null' as by default, its value is null and not "Default".

regards,
vijay.
 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In the above example, an instance of class C will be associated with an instance of enclosing class B.
At the same time, since C is a subclass of A, there will be a parent instance of A along with (or 'within' ) every instance of C.

This is the difference between "enclosing" class and "parent" class.

In the given example, it so happens that A and C are is the same class, OuterTest. Ofcourse, this doesn't mean that there is only one instance. There will certainly be two instances, one in the role of parent object and other in the role of enclosing object.


To answer the second part, since the superclass constructor runs before the subclass constructor, hence 'id" in the superclass-instance has already got the value "Default" before the value gets printed in the subclass's constructor.
[ July 27, 2006: Message edited by: Neelesh Bodas ]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The line : InnerTest inner = outer . new InnerTest ( ) ;
creats a instance of InnerTest called inner.

To create the parent class OuterTest , the no arg constructor OuterTest() is called by the super() call inserted by Compiler in the constructor of InnerTest.

In OuterTest() contructor id is assigned to " Default ".


Eleanor
reply
    Bookmark Topic Watch Topic
  • New Topic