File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructor invocation order/priority" Watch "constructor invocation order/priority" New topic

constructor invocation order/priority

Kiran Sharma

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 {
System.out.println("A.A called");
A(int i) {
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() {
System.out.println("B.B() called");

B(int 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

Joined: Apr 05, 2000
Posts: 1441
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.

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

Joined: Apr 05, 2000
Posts: 1441
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.
Kiran Sharma

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() )
It is sorta covered in the JavaRanch Style Guide.
subject: constructor invocation order/priority
Similar Threads
Priority Queue and iterator
confusion in methdo overloading with var args
static methods
a question about super command
can Interface Implement and extend?