posted 18 years ago
I am having some difficulty with the following question from Dan Chisholm:
The result of running this code is that A1B0A0B1A0B2 is printed out. But I would have expected the output to show A1B0A1B1A1B2. I do not understand how you can go from printing out A1B0 to A0. At one moment the number of A's that have been instantiated is 2, then according to the output, all of a sudden it appears as though no A's have been instantiated.
Could some one explain why these A's are 0, not 1. A1B0A0B1A0B2
For example:
A a1 = new A();
// after this line, there is one instance of A and the counter shows 1.
a1.m1();
// here there are two, because another is instantiated within the method m2(), so the A() constructor is called and the counter is incremented again to become 2.
a1.m2();
// here no A constructors are called, so I would assume that the counter remains at 2. It never gets reset explicitly, but within this method it appears as though the counter for A is back to 0.
a1.m3();
Could someone explain what is going on here, please?
Thoughts appreciated.
[ March 06, 2006: Message edited by: Arthur Blair ]