• 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 doubt

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
any one please explain how the below code results in the given answer






a)Prints: A1B0A0B1A0B2


thanks,

kishore
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exact doubt you have?
 
kishore kovil
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i need explanation for the question
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, "A a1 = new A();" will create an Object of class A, setting the private String name of this Object to A0. Then "a1.m1();" is executed - and so "new A().new B();" will be executed. A new A-object is created, setting its name to A1 and a B (tied to this A) is created with an innerCounter of 0 (incrementing the static innerCounter to 1). Then this A's name and this Bs name are printed, giving A1B0.
Next, "a1.m2();" is executed - so "new A.B();" is executed.
A new B will be created (tied to our "old" object we crerated first, referenced by a1). This B's name will be B1, incrementing the innerCounter to 2. Our A, referenced by a1, still has "his" name A0, so A0B1 will be printed.
Last, "a1.m3();" will be executed, and so "new B();".

Again, a new B will be created, tied to our first A-object, referenced by a1. So A0B2 is printed.

Hope, this will help.
 
reply
    Bookmark Topic Watch Topic
  • New Topic