• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

inner class

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


output: A1B0 A0B1 A0B2

when control reaches //2 constructor of A() will be called twice by which
the static counter is set to 1.new A.B() calls B()'s constructor inturn A.this.name
should contain a value A1 but the output says A0 how and why??


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

I think you are getting derailed by:


Notice name is not static. That means you'll have an instance of name for each instance of class A.

After this line executes:

A's name is set to "A0", although counter is post incremented to 1.

When this line runs:

A's constructor is run again, and the new instance of name is set to "A1". After that, the instance of that name dissolves.

The line:

Creates a new instance of B. A's constructor is not called because it is not necessary being that it was called when a1 was created. Here, the original value of name "A0" is used.

The same applies to the next line too.

Hope this helps...
Aloha,
Doug

-- Nothing is impossible if I'mPossible
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic