This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "constructors" Watch "constructors" New topic
Author

constructors

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi
pl see the code below :
class A {
public int Avar;
public A() {
System.out.println("AAA");
doSomething();
}
public void doSomething() {
Avar = 1111;
System.out.println("A.doSomething()");
}
}
public class B extends A {
public int Bvar = 2222;
public B() {
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();
}
}
The output is:
AAA
Bvar=0 ???
BBB
Bvar=2222
Avar=0
Can anybody tell me why the the doSomething() method invoked from within A's constructor is actually B's version of doSomething ?
Nirmala
Ranch Hand

Joined: Jun 28, 2000
Posts: 93
I got also the same answer ,but I m not clear on the flow of super class and subclass COnstructors,any deep explanation,please.
Thanks
Nirmala
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Hi,
Let me try to help u guys.
new B(); statement call the no argument constructor of class B. In the constructor of class B there is no call to "super()" the compiler put a default call to (super()) the construtor of class A.
In the constructor of class first it prints "AAA" then it call doSomething method. because we are overriding the doSomething() method of class A in class B. so B's version of doSomething() is called. so when it execute the doSomething method in class B. method try to print a instance variable of class B. here comes something importent. all the instance variables of any class are initialize after the execution of the construtor of parent class.
since method doSomething of class B is called from constructor of A so all the variables of class B has not been initialized. that is why it prints '0' afteter printing "AAA".
after returning fron constructor of class A first of all initialzer of class B take place and initialize all the instance variable. then continue execution in the constructor of class B.
first prints "BBB". then call doSomething method of class B and by now instance variable of class B has initialized so it prints "2222". then it prints the variable Avar( inherited from class A) which has default value to zero. so it prints '0'.
Hope i can express myself and help u guys. let me know if something is wrong.
vivek
[This message has been edited by Vivek Shrivastava (edited July 15, 2000).]
Nirmala
Ranch Hand

Joined: Jun 28, 2000
Posts: 93
Thank you so much vivek and it's clarifies my doubt.
Thanks again.
Nirmala
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Vivek ,If in the constructor of class A , the overridden method of class B is being called and instance variables of class B are initialized only after constructor of parent class.
Then why it is printing "Bvar=0" ,it should generate some error , as variable Bvar should not be known this time.
I am getting confused , could you pl. explain it more clearly....
thanx in advance ,
DS
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Deepak,
This has to do with order of initialization of classes.
Whenever a new class instance is created, memory space is allocated for it with room for all the instance variables declared in the class type and all the instance variable declared in each superclass of the class. This happens even before the constructors are executed. The constructors are executed in the "parent-first" order, and in each case instance initializer(s) and instance variable initializer(s) for the class is executed.
In our example, when the constructor of A is being executed, though variables of B are not yet initialized, they have been allocated memory, initialized to their default value( zero for integral types ) and have been resolved. Hence it prints 0.
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
thanks a lot, Ajith.
this is really a new concept for me.
thanks again,
DS
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructors
 
Similar Threads
Overridden method invoked in the constructor. Please clear it for me.
constructors
Help me on this "constructor" question!
difficult Construction
why this happend in constructor?