aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Initialization Blocks Question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Initialization Blocks Question" Watch "Initialization Blocks Question" New topic
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Initialization Blocks Question
 
Similar Threads
Initialization Blocks
Init Blocks?
static and instance init blocks
instance init block concept
Execution order of Static Blocks, Constructors, and Instance Blocks