Tuesday, January 23, 2007

inner class doubt

First, "A a1 = new A();" will create an Object of class A, setting the private String name of this Object to A0. Then "a1.m1();" is executed - and so "new A().new B();" will be executed. A new A-object is created, setting its name to A1 and a B (tied to this A) is created with an innerCounter of 0 (incrementing the static innerCounter to 1). Then this A's name and this Bs name are printed, giving A1B0. Next, "a1.m2();" is executed - so "new A.B();" is executed. A new B will be created (tied to our "old" object we crerated first, referenced by a1). This B's name will be B1, incrementing the innerCounter to 2. Our A, referenced by a1, still has "his" name A0, so A0B1 will be printed. Last, "a1.m3();" will be executed, and so "new B();". Again, a new B will be created, tied to our first A-object, referenced by a1. So A0B2 is printed.First, "A a1 = new A();" will create an Object of class A, setting the private String name of this Object to A0. Then "a1.m1();" is executed - and so "new A().new B();" will be executed. A new A-object is created, setting its name to A1 and a B (tied to this A) is created with an innerCounter of 0 (incrementing the static innerCounter to 1). Then this A's name and this Bs name are printed, giving A1B0. Next, "a1.m2();" is executed - so "new A.B();" is executed. A new B will be created (tied to our "old" object we crerated first, referenced by a1). This B's name will be B1, incrementing the innerCounter to 2. Our A, referenced by a1, still has "his" name A0, so A0B1 will be printed. Last, "a1.m3();" will be executed, and so "new B();". Again, a new B will be created, tied to our first A-object, referenced by a1. So A0B2 is printed.

SOURCE : www.referjava.com

No comments: