• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Doubt on Inner class

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am kind of confused here. Here is the program:



It prints A1B0A0B1A0B2. I understood until how it prints A1B0 , after that I thought it would print A1B1A1B2, but it prints A0B1A0B2. How?
When a1.m2() executed, A.this.name would be A1, correct ? I don't understand when it became A0. Please help me to understand the code correctly.

Thank you so much.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chitra,
I suppose it goes like this.Initially counter and innerCounter are both set to 0.In the main method a new object of A is created.So the constructor of class A sets variable name to A0 and sets outer to 1..So at this point outer is 1 and innerCounter is 0.Next in the main ,m1 is called on object of class A.In m1 a new object of A is created setting name to A1 and incrementing outer to 2.Method m1 calls constructor of inner class on this newly created object of class A.Constructor in class B sets name in inner class to B0 and increments innerCounter to 1.In the B's constructor the println statement prints A1 and B0.Its A1 because in method m1 its the new object of the enclosing class on which the constructor of B gets called.In method m2 and m3 its the first object of A on which constructors of B is called.I hope you will be able to figure it out now.If you still find this confusing...let me know.

all the best
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try making 'name' variable of class A as 'static' and see the output !!

Thanks,
Lalitha.
 
Chitra AP
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it. Thanks.

By the way, you can find free mock exams at www.witscale.com
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
good question
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question chitra, tricks the brain to catch a static variable with a non static variable. I understood only after seeing the output, here it is :

Counter = 0
Inside m1
Counter = 1
A1B0
Inside m2
A0B1
Inside m3
A0B2

Thanks guys,
San
[ June 08, 2005: Message edited by: San Sreeds ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic