• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Initialization Blocks Question

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic