File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q on Inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q on Inner classes" Watch "Q on Inner classes" New topic

Q on Inner classes

Lakshmi Saradha
Ranch Hand

Joined: Oct 21, 2003
Posts: 170
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( + 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)

Thanks,<br />Lakshmi.
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Hi Lakshmi, this question has been asked here.
Lakshmi Saradha
Ranch Hand

Joined: Oct 21, 2003
Posts: 170
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?
I agree. Here's the link:
subject: Q on Inner classes
It's not a secret anymore!