• 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

Inner class constructor

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

The above code prints
STP
STP
In the above code inner class constructor InnerTest() doesn't get invoked? Shouldn't it print
STP
Default
Thanks
veena
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Veena,
I think the inner class constructor does get called. I added the following line in the Inner class constructor and it was printed.
System.out.println("inner class constructor");

Thanks,
Cathy.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the InnerTest instance there is a id field inherited, however Outer.this.id is also accesible. The latter is a field of the containing class, it will be initialized by a constructor of its class: new OuterTest("STP"). The former would be intialized by InnerTest constructor, either directly (id="something" or indirectly by calling super();
Try System.out.println(OuterTest.this.id +"\t"+ id);
and you will see
STP Default
STP Default
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jose,
I cannot figure out how calling super() changes the inherited id variable? Can you please explain this concept.
Thanks,
Cathy.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
super() calls the no-args (default) constructor of the superclass, in this case, OuterTest().
Inside this OuterTest() constructor, the variable id of InnerTest is assigned to "Default", because 'this' is referring to the current object.
So OuterTest's id is still "STP".
Hope you understood.
Ana
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the output is the same even if I take the "this" out..
OuterTest ( ) {
//this.id = " Default " ;
id = " Default " ;
}
Now the id variable is refering to Inner or outer? I am totally lost..
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Ana.
____________________________________________________
Cathy, within a constructor or instance method id is equivalent to this.id .
this is the hidden argument to instance methods representing the object on which the method was called: "aCar.go();" within go() this refers to aCar.
this in a constructor is the object being initialized, that is the object created by new.
 
I guess everyone has an angle. Fine, what do you want? Just know that you cannot have 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