I didn't understand the following program

out put of the following program is

A0

A1

A1B0

A0B1

A0B2

In the above bold content i didn't understand, how A0 is coming. I am expecting null instead of A0. But here i am getting some other value. How its coming? please explain

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();

System.out.println(name);

}

class B

{

private String name;

B()

{

name = "B" + getInnerCounter();

System.out.println(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();

}

}

[1023 byte] By [Javalovera] at [2007-11-27 9:06:28]
# 1
1. Please format your code as per http://forum.java.sun.com/help.jspa?sec=formatting2.You're invoking methods m2 and m3 on the a1 instance of A. That instance has name set to "A0". Consequently you get "A0B1" and "A0B2".
dwga at 2007-7-12 21:41:55 > top of Java-index,Java Essentials,Java Programming...
# 2
why would you expect null to be returned from a method that returns a numerical value?
jwentinga at 2007-7-12 21:41:55 > top of Java-index,Java Essentials,Java Programming...
# 3

> why would you expect null to be returned from a

> method that returns a numerical value?

Well he doesn't. I think you misunderstood his question.

He was expecting A's name to be null, which isn't the case since it has been initialized to "A0" in the first A() constructor call.

dwga at 2007-7-12 21:41:55 > top of Java-index,Java Essentials,Java Programming...
# 4
thanks a lot. Now i got it. I forgot about that a1 instance.
Javalovera at 2007-7-12 21:41:55 > top of Java-index,Java Essentials,Java Programming...