wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Why first of all it runs constructor of class B? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why first of all it runs constructor of class B?" Watch "Why first of all it runs constructor of class B?" New topic
Author

Why first of all it runs constructor of class B?

Supun Lakshan Dissanayake
Ranch Hand

Joined: Oct 26, 2012
Posts: 132

When i compile and run Demo.java program the output is

Creating object B
Static Block of class A
Static Block of class B
Instance Block of class A
Parameterized Constructor of class A
Instance Block of class B



I need to know why constructor method of class B runs first of all.
According to my knowledge JVM runs programmes according to following order

when we load a java template to RAM(Heap memory)
1. initialize static variables
2. executing static blocks
by static initializer

when we creating a object using loaded java template
1. initialize instance variables
2. executing instance blocks
3. executing Constructor method
by object initializer


Are you better than me? Then please show me my mistakes..
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7677
    
  19

Supun Lakshan Dissanayake wrote:I need to know why constructor method of class B runs first of all.

Because you asked it to.

What do you think:
new B();
asks the JVM to do?

...when we load a java template to RAM...

I'd avoid words like RAM when describing the actions of a JVM. A generic term like "memory" or "the heap" is OK, but I definitely wouldn't think of it as contiguous, as it often is in languages like C or C++.

It seems to me like you've described the action order quite comprehensively, so I'm not quite sure where your confusion is.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Supun Lakshan Dissanayake
Ranch Hand

Joined: Oct 26, 2012
Posts: 132

Winston Gutkowski wrote:What do you think:
new B();
asks the JVM to do?


I think static initializer and object initializer perform their tasks to following order

01. initialize static variable(s) of super class
02. execute static block(s) of super class
03. initialize static variable(s) of sub class
04. execute static block(s) of sub class
05. initialize instance(non-static) variable(s) of super class
06. execute instance block(s) of super class
07. execute constructor method of super class <<< please pay attention
08. initialize instance(non-static) variable(s) of sub class
09. execute instance block(s) of sub class
10. execute constructor method of sub class <<< please pay attention

How JVM execute constructor method of super class before executing constructor method of sub class ?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7677
    
  19

Supun Lakshan Dissanayake wrote:How JVM execute constructor method of super class before executing constructor method of sub class ?

Well, in your case, it's fairly obvious, because the first statement in your B constructor is:
super(100);

However, it will normally happen even if you don't explicitly call it, because if you don't, the compiler will add a call to:
super();
for you.

Which is why, if you intend writing a class that is publicly extendable, you should generally supply at least a protected no-args constructor.

Winston
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4180
    
  21

I don't think you included enough steps in your class initialization. If we take just the class initialization part (not the static part) then the details (as described in JLS 12.5) would be:

1: Evaluate an instance creation statement (JLS 15.9)
-- Part of this means creating the instance variables and assigning them their default values.
2: Evaluate constructor arguments into variables
3a: IF the first line of the constructor is a call to this(...) [another constructor in same class], execute that constructor
3b: ELSE there is no explicit call to another constructor in the same class so:
3b1a: IF the first line of he constructor is a call to super(...) [an explicit call to a constructor in the super class], execute that constructor
3b1b: ELSE there is no explicit call to super class constructor, implicitly call super() [the no-argument constructor of the super class]
3b2: Execute instance initializers and instance variable initializers in left-to-right, top-to-bottom order.
4: Execute the rest of the constructor body.

What is important is that there is a special meaning for the first line in the constructor's body. If it is this(...) then it calls a different constructor in the same class, if it is super(...) it calls the corresponding constructor in the super class, and if it is anything else (or nothing at all) then super() is inserted (the no-args constructor of the super class). Only this first line of a constructor is executed before initialization, because it makes sure the super class is initialized before the child class.


Steve
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18750
    
  40

Supun Lakshan Dissanayake wrote:
Winston Gutkowski wrote:What do you think:
new B();
asks the JVM to do?


I think static initializer and object initializer perform their tasks to following order

01. initialize static variable(s) of super class
02. execute static block(s) of super class
03. initialize static variable(s) of sub class
04. execute static block(s) of sub class
05. initialize instance(non-static) variable(s) of super class
06. execute instance block(s) of super class
07. execute constructor method of super class
08. initialize instance(non-static) variable(s) of sub class
09. execute instance block(s) of sub class
10. execute constructor method of sub class


There are a few things wrong with this list -- and I thought that it should be mentioned...

First, as mentioned in your other topic, static initializers and initialization of static variables are done in the order that they are encountered in code. In your example, the variable is initialized first because it is declared first -- and it is not a hard rule as you specified. Also, this holds true for instance initializers and the initialization of instance variables.

Second, static initializers and initialization of static variabes are done when the class is loaded. In your example, it is done in that order because you are creating an instance of the subclass. It is actually possible to load the subclass without loading the superclass -- such as calling a static method of the subclass. So again, it is not a hard rule. Also, if you have a weird setup, such as the superclass containing an instance of the subclass, you can have weird class load ordering too.

And third, the JLS defines the ordering (for the instance portion) in slightly different terms -- and although you are saying the same thing (for that portion), perhaps it would be good to use the same terms... search the ranch for previous discussions.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
wood burning stoves
 
subject: Why first of all it runs constructor of class B?