• 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 Question - Baffling

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I have this question from Dan's study guide from the chapter on Nested classes and whose correct answer is baffling me. Hope somebody can throw some light on this.

The correct answer is
A1B0A0B1A0B2 (which I checked to be true)
but what I was thinking is
A1B0A1B1A1B2

My question more specifically is that why after Line 3 gets executed, a1.name = A0. Shouldn't it remain the previous value of "A1" ???
Any suggestions? I am surely missing something here !! Thanks.

Roopesh.
[ August 31, 2005: Message edited by: Michael Ernest ]
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output is like that because the methods m2() and m3() are invoked on the instance a1 whose name is "A0".

Kayal
 
Roopesh Gulecha
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kayal,
Sorry but that did not help.
What do you mean that methods m2() and m3() are invoked on a1 whose value is "A0"?
Isn;t the field of enclosing class, private string name = "A1" after Line1 gets executed.

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


The correct answer is
A1B0A0B1A0B2 (which I checked to be true)
but what I was thinking is
A1B0A1B1A1B2

My question more specifically is that why after Line 3 gets executed, a1.name = A0. Shouldn't it remain the previous value of "A1" ???



Hi Roopesh,

U try to observe one thing in the above code.Can u have look at the line //2 & //3?.Do u find any difference between those two stmts?.Yes there is.

void m1() {new A().new B();}

In the above method, after invoking method m1 using an instance of A(with name A0) one more new instance is created whose name is set to A1.
Now instance of B is created using this instance(with name A1) so the output after this method call is A1B0.

void m2() {this.new B();}// 3

In the above method,after invoking method m1 using an instance of A(with name A0) u can observe no new instance of A is created and also new instance of B is created using an already existing instance of A(with name A0.Yes "this" in the above stmt refers to the instance "A0" only not "A1").So the output is A0B1.

void m3() {new B();}// 4

Similarly in the above line //4 also the stmt would be expanded as this.new B()by the compiler(i.e., will be executed as this.new B().even though "this" reference is not explicitly mentioned by you) & it gets executed.Hence the output is A0B2.

So the final output is A1B0A0B1A0B2.

Hope it helps!!.

Regards,
Priya.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roopesh,

Just renaming the method/class names would tell you what's happening, point to remember is that your counter variables are static.




This is what the output looks like:
====================================================
Outer.createNewInnerAndOuter(): Outer1::Inner0
Outer.createNewInnerWithCurrentOuter(): Outer0::Inner1
Outer.createNewInner(): Outer0::Inner2
====================================================

Hope this helps !!!
Regards,
Jimmy
 
Roopesh Gulecha
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Priya and Jimmy..
I get it now (somehow was hard to get in earlier)
Well planning on taking the 1.4 exam in about 8 to 9 days. I am sure it will go good after a lot of mock tests in these days ...

Roopesh.
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what I fail to see is, why a static variable is getting re-initialised?
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Static variable was never reinitialized it was that the String Object created earlier was called later to print thats it,if anyone needs a clear explanation to this question i would be grateful to provide it.

With Lots Of Luck,
Anand
 
reply
    Bookmark Topic Watch Topic
  • New Topic