Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

Puzzling Dan Chisholm question on inner classes

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having some difficulty with the following question from Dan Chisholm:



The result of running this code is that A1B0A0B1A0B2 is printed out. But I would have expected the output to show A1B0A1B1A1B2. I do not understand how you can go from printing out A1B0 to A0. At one moment the number of A's that have been instantiated is 2, then according to the output, all of a sudden it appears as though no A's have been instantiated.

Could some one explain why these A's are 0, not 1. A1B0A0B1A0B2

For example:
A a1 = new A();
// after this line, there is one instance of A and the counter shows 1.

a1.m1();
// here there are two, because another is instantiated within the method m2(), so the A() constructor is called and the counter is incremented again to become 2.

a1.m2();
// here no A constructors are called, so I would assume that the counter remains at 2. It never gets reset explicitly, but within this method it appears as though the counter for A is back to 0.

a1.m3();

Could someone explain what is going on here, please?
Thoughts appreciated.
[ March 06, 2006: Message edited by: Arthur Blair ]
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Arthur,

First of all thanks for a really good question on Inner classes. Dan really lived up to his fame of quality questions. The the basic confusion in this question is the object creation part for Class A. If you keep track of the object created and there value of private instance variable 'name', you will get the key to the problem. The object created by statement
A a1 = new A()
have name = "A0". This object is used to call method m1, which in turn creates a new object of class A which will have name = "A1" , since counter has incremented to 1 now. Now this object (with name="A1" ) is used to create object of inner class B -
new A().new B()
So we get "A1B0" on the console for the constructyor of this object.
Now the same old object created earlier i.e. 'a1' (with name = "A0") is used to call methods m2 & m3. In both methods this 'a1' is used to create the new inner class objects so we get "A0B1" for method m2 and "A0B2" for method m3. See method m3(). It is not having any reference to the current object of outer class A, but by default it uses 'this' since we always require a outer class object reference to create an inner class object. So both m2 & m3 are using the the same object 'a1' hence we get "A0" back.

Hope I was able to clear your confusion. If not reply frankly. I'll try to explain in more elaborate way.
[ March 06, 2006: Message edited by: Rahul Ghodeswar ]
 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it has clicked now! Thank you for your explanation, Rahul.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
make the variable "name" in class A static
then u will get the result u think u get
since name is not static hence each object will have its own copy
 
Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic