wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes constructor invocation order/priority Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructor invocation order/priority" Watch "constructor invocation order/priority" New topic
Author

constructor invocation order/priority

Kiran Sharma
Greenhorn

Joined: Dec 09, 2000
Posts: 5
Can any one plz. clarify my concepts on constructor invocation order/priority with reference to following code.

-------------------------
class A {
A()
{
System.out.println("A.A called");
}
A(int i) {
this();
System.out.println("A.A(int) called");
}
}

class B extends A {
int i = f();
int j;

{
j = 37;
System.out.println("initialization block executed");
}

B() {
this(10);
System.out.println("B.B() called");
}

B(int i) {
super(i);
System.out.println("B.B(int) called");
}

int f() {
System.out.println("B.f called");
return 47;
}
}

public class CtorDemo {
public static void main(String args[]) {
B bobj = new B();
}
}
1 . Why does the C'tor A(int) get preference over A() in class A
while B() over B(int) in class B ?
2 . Also if I use this() in A() instead of A(int) C'tor
(or in B(int) instead of B()) i get "Recursive constructor invocation"
error , plz clarify my this() funda .
Rob Acraman
Ranch Hand

Joined: Dec 03, 2000
Posts: 89
1) No constructor gets priority! You need to look at your "this" calls:
- In A(int), you call "this()"(with no arguments) before you do the println, so naturally the constructor with no arguments is run at that point.
- In B(), you call "this(int)" before the println, so again the
B(int) constructor is run at that point.
So the only priority operating is that which you've put in place by deciding which constructors call which others.

2) The definition of "recursive" is a method that calls itself. This is a potentially dangerous practice, since it can easily lead to infinite loops. For this reason, Java disallows recursion in constructors - ie. constructors cannot call themselves.
This is what's happening with your two examples:
In A(), putting a call to "this()" (naturally, as the first staement), then since "this" *IS* "A" would result in A() calling A() - calling A() calling A() calling .....
Ditto for B(int), where "this(int)" becomes B(int) calling B(int) calling .....
Hope this helps

asheesh talwar
Ranch Hand

Joined: Dec 10, 2000
Posts: 31
I am not able to understand why after executing the print statements in constructor of class A it is not printing
B.B(int)called and B.B() called coz the control of the program goes to class A constructor from class B constructor(with arguments).
I want to why it is first printing B.f called and initialization block executed ?
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1449
Because the initialization block in a class is executed when the class is loaded by the class loader. So you see the output from the init block before a B constructor is called.
John


The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1449
Oops...
I had "static" on the brain for some reason. A non-static init block will be executed when an instance is created before a constructor is invoked. So you will see the init block output before the B constructor's output.
Static vars are initialized and static init blocks are executed when the class is loaded.
John
Kiran Sharma
Greenhorn

Joined: Dec 09, 2000
Posts: 5

Thanks guys ! That did solve my second querry , but @ the first
I wud like to know what forms the basis of the C'tor A(int i)
in class A being invoked & not A ().
( A () is invoked by A(int i) thru this() )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructor invocation order/priority