• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initialization Blocks Question

 
Srinivas Palam
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 1329
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic