so the first thing to do is to create a new B(); The constructor of B seems to start with insertYolk(new InnerB());
but there is also the invisible call: super();
before, so before any yolk action is done, the constructor of A runs saying: super(); // invisible call of class Object's constructor, but no output, so I'll ignore this and then System.out.println("New A()");
This seems to be the first line printed, but again something was forgotten: The line private InnerA y = new InnerA();
also belongs to the constructor, and the initialization of variables comes before the typed-in code in the brackets of the constructor, so we are still with no output and have to do a new InnerA();
Constructor of InnerA runs. InnerA has also only Object as super class, so you can ignore the call of super() again - at least regarding output.
And really, this is the first line printed. The constructor is over, and we jump back were we came from, the constructor of A and now: System.out.println("New A()"); New A() is printed.
We came from the constructor of B, so back there and we have a method call: insertYolk(new InnerB());
You have to construct an innerB object now... I think, from here, you can follow the rest of the flow by yourself.
all events occur in real time
Joined: Feb 14, 2007
Thank you for the in-detail explanation.
But still doubt with the line e2.g() how it is printing B.innerB.f()
Because when e2.g() is called,g() in A is called [Because class B is not having the method g()] and which inturn calls y.f().
AS y is of type 'Inner class A" ,f() in "inner class A" should be executed ,and should print "A.iinerA.f()
How it is printing B.innerB.f().
If I sound silly,iam very sorry.Really I tried hard to get it by myself,But atlast landed here again for the clarificatio.
Joined: Aug 25, 2006
e2.g(); // calls: y.f();
the y is of type InnerA but the object that it refers to is an InnerB object that was created during the process I quite short described at the end of my first post. It was in the line insertYolk(new InnerB());
So we have this innerB object already and now the f() method is called with it. In InnerB method f() is overridden as
And this is just what it prints.
Little hint: To seperate what prints what, maybe you should put a System.out.println("---------");
between the first and second line of the main method. You'll see that the line e2.g();