| Author |
Initialization Blocks Question
|
Srinivas Palam
Ranch Hand
Joined: Oct 20, 2005
Posts: 39
|
|
When I ran the class, output was
D:\Java>java Init
1st static init
2nd static init
in main method
1st instance init
2nd instance init
no arg Const ---> when the constructor(Init) was invoked, I though this will print, but it was printing 1st instance init. Please explain.1st instance init
2nd instance init
1 arg Const
Please explain.
|
 |
Himai Minh
Ranch Hand
Joined: Jul 29, 2012
Posts: 292
|
|
When new Init() is invoked, the implicit super() inside Init() contrustor is called.
Then, the initialization blocks are called and prints 1st instance init and 2nd instance init.
Last, execute the print and print out no arg const.
|
 |
Srinivas Palam
Ranch Hand
Joined: Oct 20, 2005
Posts: 39
|
|
|
Thanks for your Reply. I am assuming the same when Init(7) is involved. It will invoke its implicit super(with one argument).
|
 |
Alan Cowap
Greenhorn
Joined: Oct 16, 2012
Posts: 6
|
|
Srinivas Palam wrote:Thanks for your Reply. I am assuming the same when Init(7) is involved. It will invoke its implicit super(with one argument).
No, the implicit call from a subclass constructor is to the super() no-args constructor.
If there is no no-args constructor in the superclass then compilation will fail.
You can explicitly call a specific superclass constructor from the subclass constructor, and that call must be the first statement in your subclass constructor.
Regards,
Al
|
This is my .sig. There are many .sigs like it, but this one is mine.
|
 |
 |
|
|
subject: Initialization Blocks Question
|
|
|