• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Please Explain working of this program.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am new to this form. I came up across this code in Head First Java, if someone could explain my small doubt, I would really appreciate it.
Code goes like this-


I got output as-
super static block
static block 10
in main
super constructor
constructor

What I could not understand is.., why there is "super constructor" in second last line. As the code is neither called explicitly or implicitly...
 
Ranch Hand
Posts: 62
Netbeans IDE MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The no argument superclass constructor is called implicitly from the subclass constructor where you do not instruct otherwise using this() or super(args)

Take a look at the note towards the bottom of this page
 
Prateek Singla
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So.. Is it like.. first statics, gets initialized irrespective of the fact from where they come from in inheritance tree and then, main method code starts and then before any object is instationized the no argument constructor runs...
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prateek Singla wrote:So.. Is it like.. first statics, gets initialized irrespective of the fact from where they come from in inheritance tree and then, main method code starts and then before any object is instationized the no argument constructor runs...


If you don't call another constructor yourself; and that's the important part. If you explicitly call a superclass contructor yourself then that will be run instead - and before the constructor for the subclass (which is why it MUST be the first statement in your constructor).

As to instantiation, it gets a bit tricky (at least it used to be; it's possible that the various changes to the memory model have simplified things), but in general you can take it as read that if you write new Whatever(), the object is not fully instantiated until the constructor for Whatever has completed (and don't forget that it may have to call superclass constructors first).

To be honest, the nuts and bolts are probably best left to memory architects, who are a rare breed of geek. If you concentrate on writing clear, uncomplicated, dumb code, the chances are you will never need to worry about it.

Winston
 
Destiny's powerful hand has made the bed of my future. And this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic