• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Dan's Inner class question...

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

Dan's question was: What will be the result if u run the following programme:


The O/P is: A1B0A0B1A0B2

My doubt is At - comment no 5. A's constructor will be incremented then at comment 6 it will be incremented again - making A.name = "A1" (counter would be 2 at that point). Now at comment 7 (where m2() is called) a new A's instance will **NOT** be created -but still A.this.name = A1 -- But look at the o/p (A1B0 *A0* B1 *A0* B2).

I understood the B1 B2 thing but A should remain A1. How come it become A0 back again.

Dan just explained that a new instance of A will not be created at comment 3 and 4.

Please clear this doubt.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure that I can clear your doubt, however, when I walked through the program by hand I got the same result that you indicated. (This is pretty lazy, I should run it too!)

Anyway, we start out with a static variable counter for A (static-A-counter) initialized to 0, and a static variable innerCounter for A (static-A-innerCounter) initialized to 0.

Static initialization occurs, I believe, at either the time a static method is accessed or a the class is instanced. Better look into this.

In any event, walking through the code line by line:

A a1 = new A(); // 5

At this point we have created A. The a1.name is set to A0, and then the counter is incremented to 1.

a1.m1(); // 6 - calls void m1() { new A().new B(); } //2

Now we create a new instance of A, on which we call new for B. This new instance of A I will call anonymous1. So for anonymous1, the name is set to A1, and then the static-A-counter is incremented to 2. Now we invoke the constructor on B. This sets its own name to B0, and then increments static-A-innerCounter to 1. Then it prints out "A1B0".

a1.m2(); // 7- calls void m2() { this.new B(); } //3

Okay now we have come back to A1. It still has the same name- A0. We create B on this instance. The constructor in B sets the name to B1 (the value of static-A-innerCounter) and then increments it to 2. It prints out "A0B1" (the name of a1 plus the name of the new B class.


a1.m3(); // 8 - calls void m3() { new B(); } //4

Again, we are utilizing A1. It has the name A0. We create B with this instance. The constructor in B sets the name to B2 (the value of static-B-innerCounter) and then increments it to 3. It prints out "A0B2").
 
Bharat Roy
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tom,

OK thanks, I guess I got it. See if I am getting it correct. At //5 we created an instance and made A0 then at //6 we created another instance of A() BUT WE DIDN'T SAVE IT. So it became A1B0. Then we came back to //7 ----Now we are back to the 1st object whose A.name was set to A0 at the beginning. (I guess, here I was getting it wrong) Hence it is still A0 and again at //8 it is still the same object so A0.

Thanks.
 
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic