| Author |
why this happend in constructor?
|
tc king
Greenhorn
Joined: Nov 10, 2006
Posts: 11
|
|
the result is: AAA Bvar=0 BBB Bvar=2222 Avar=0 why? thanks in advance [HENRY: Formatted Code] [ November 20, 2006: Message edited by: Henry Wong ]
|
 |
James Quinton
Ranch Hand
Joined: Oct 02, 2006
Posts: 94
|
|
are you sure you gave the correct code sample? your output doesn't make any sense at all.
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
Yes, the output makes sense. I'll list the steps that take place. 1. new B() causes the no-args constructor of B to be called. Since there is no explicit invocation of a constructor of A, the no-arg constructor, A(), is called. 2. In A(), AAA is printed. Then there is a call to doSomething(). 3. Since this method is overidden in the subclass, the overridden version is called. At this point, none of the instance variables in class B have been initialized so they have their default values. So in B Bvar=0 is printed. 4. Now the instance variable initializers in class B are executed. 5. The rest of the constructor body in B() is executed. 6. BBB is printed. 7. Now doSomething() is called and prints in B Bvar=2222 is printed. 8. The last line in the constructor of B prints Avar=0 since Avar was never assigned a value since the doSomething() method of A is not executed. [ November 20, 2006: Message edited by: Keith Lynn ]
|
 |
James Quinton
Ranch Hand
Joined: Oct 02, 2006
Posts: 94
|
|
maybe I am too picky, Keith. But compare your output, the original output doesn't make sense.
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
|
James, it does make sense. From this one example above, you get to know the flow how classes get loaded and how instance variables gets initialized. Good to bring up this question here.
|
SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
|
 |
James Quinton
Ranch Hand
Joined: Oct 02, 2006
Posts: 94
|
|
Originally posted by Jothi Shankar Kumar Sankararaj: James, it does make sense. From this one example above, you get to know the flow how classes get loaded and how instance variables gets initialized. Good to bring up this question here.
it doesn't make sense with this output: AAA Bvar=0 BBB Bvar=2222 Avar=0 guys, look at it close, very close. [ November 20, 2006: Message edited by: James Quinton ]
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
Except for leaving out the phrase in B, that is the exact output you get if you interpret B. Why do you think it doesn't make sense James?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16692
|
|
Originally posted by James Quinton: it doesn't make sense with this output: AAA Bvar=0 BBB Bvar=2222 Avar=0 guys, look at it close, very close.
James, When I compile and run this, I get...
AAA in B Bvar=0 BBB in B Bvar=2222 Avar=0
Quite frankly, we can not tell what parts of it doesn't make sense to you, if you don't tell us. Henry [ November 20, 2006: Message edited by: Henry Wong ]
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
James Quinton
Ranch Hand
Joined: Oct 02, 2006
Posts: 94
|
|
Originally posted by Keith Lynn: Except for leaving out the phrase in B, that is the exact output you get if you interpret B. Why do you think it doesn't make sense James?
the phrase "in B" is what I meant "doesn't make sense". in exam, as soon as I saw this kind of thing, I chose the easiest answer and went to the next one.
|
 |
Satish Kota
Ranch Hand
Joined: Feb 08, 2006
Posts: 88
|
|
|
Can some one please explain why is it that Class B doSomething method is getting invoked instead of Class A's?
|
SCJP 5.0 77%
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Hi Satish, Can't you see inside main method, you are saying new B() and not new A()???and also doSomething() is overriden???
|
 |
tc king
Greenhorn
Joined: Nov 10, 2006
Posts: 11
|
|
Hi Keith your explaination 3,why in A() called overridden version of doSomething()? at that time the instance of B is not construt yet.
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
This is the discussion about this from the Java Language Specification 12.5. [I]Unlike C++, the Java programming language does not specify altered rules for method dispatch during the creation of a new class instance. If methods are invoked that are overridden in subclasses in the object being initialized, then these overriding methods are used, even before the new object is completely initialized. Thus, compiling and running the example: produces the output: 0 3 This shows that the invocation of printThree in the constructor for class Super does not invoke the definition of printThree in class Super, but rather invokes the overriding definition of printThree in class Test. This method therefore runs before the field initializers of Test have been executed, which is why the first value output is 0, the default value to which the field three of Test is initialized. The later invocation of printThree in method main invokes the same definition of printThree, but by that point the initializer for instance variable three has been executed, and so the value 3 is printed.[/I]
|
 |
Ravindranath Chowdary
Ranch Hand
Joined: Nov 08, 2006
Posts: 71
|
|
Hi Friends, In the code the i am writing how the code executes line by line with explanation. class A { public int Avar; public A() { super(); System.out.println("AAA"); doSomething(); } public void doSomething() { Avar = 1111; System.out.println("A.doSomething()"); } } class B extends A { public int Bvar = 22; public B() { super();//calls the A() constructor System.out.println("BBB"); doSomething(); System.out.println("Avar=" + Avar); } public void doSomething() { System.out.println("Bvar=" + Bvar); } public static void main(String[] args) { new B(); } } Steps of execution from main():- 1.calling the B() 2.First statement in a constructor is super() it calls the constructor of A 3.Then is prints the 'AAA'. 4.calling doSomething() will call the doSomething() of class B because your creating an object to class B(concept of polymorphism) 5.prints Bvar=0. The value of Bvar = o bcoz all the initializations will be done after the super() and before the second statement is executed. I will assure you that you will get an initialized value i.e, 22 if you have printed the value of Bvar after the super() is done. 6.completed the A() and comeback to B(). 7.Prints BBB. 8.Calls doSomething() and prints Bvar=22 9.prints Avar, it is an instance varible which has the default value of 0 if it is not initialized. I think the steps are clear and you have understood your output is like this. Thanks, Ravindranath.
|
 |
 |
|
|
subject: why this happend in constructor?
|
|
|