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);
}}
void m1() {new A().new B();} // 1
void m2() {new A.B();} // 2
void m3() {new B();} // 3
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?
Answer : Prints: A1B0A0B1A0B2
The counter variable in class A is static. This static counter variable becomes 1 after line 1 is executed. After line 2 is executed it becomes 0 again. how is this possible? Since it is a static variable it should have been 1. According to my understanding, the answer should have been A1B0A1B1A1B2 which i know is the wrong answer.