• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic