Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q on Inner classes

 
Lakshmi Saradha
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}}
The ans given is A1B0A0B1A0B2.

How is AO printed after the first A1B0(The AO betwen slashes?Shoud it not print A1?(A1B0/A0/B1A0B2)
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lakshmi, this question has been asked here.
 
Lakshmi Saradha
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vad,
Thank you for the link. I think I understood.
In case of line 2, A0 is being printed. Is it because of the reason that the instance of B is not associated with any instance of A at that point?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic