Hi all,
Please refer question 12:
http://www.danchisholm.net/july21/topic/section6/nested1.html Question 12
class A {
private static int counter;
public static int getCounter(){return counter++;}
private static int innerCounter;
public static int getInnerCounter(){return innerCounter++;}
private
String name;
A() {name = "A" + getCounter();}
class B {
private String name;
B() {
name = "B" + getInnerCounter();
System.out.print(A.this.name + name); // 1
}}
void m1() {new A().new B();} // 2
void m2() {this.new B();} // 3
void m3() {new B();} // 4
public static void main(String[] args) {
A a1 = new A();
a1.m1(); a1.m2(); a1.m3();
}}
What is the result of attempting to compile and run the program?
a. Prints: A0B0A1B1A1B2
b. Prints: A0B0A1B1A2B2
c. Prints: A1B0A0B1A0B2
d. Compile-time error at line 1
e. Compile-time error at line 2
f. Compile-time error at line 3
g. Compile-time error at line 4
h. Other compile-time error.
i. Run-time error
j. None of the above
Answer to Question 12: = C Prints: A1B0A0B1A0B2
http://www.danchisholm.net/july21/topic/section6/nested1ans.html ------------------------------------------------------------------
My question is does the line "A a1 = new A();" produces "A0"? If so, why is it not printed out? The answer starts with "A1......"
Also I do not understand the explanation provided on the answer. Why is it that line 3 - "void m2() {this.new B();}" calls "A1" and not "A0"?
Thank you.