File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor invocation order/priority

 
Kiran Sharma
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1449
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
John Wetherbie
Rancher
Posts: 1449
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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() )
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic