• 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

Why first of all it runs constructor of class B?

 
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
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

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
 
Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
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

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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

reply
    Bookmark Topic Watch Topic
  • New Topic