Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

init blocks and constructor chain

 
Ken Truitt
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds stupid, but what is the difference between new B() and the
compiler-supplied call to B(), and to A(), in the code below. You type
java C, and main runs in c, instantiating a C--C's default constructor
runs, calls super(), which in turn calls super() (in A), which calls
super() (the Object constructor), which completes and control returns to A's constructor, A's members are initialized, A() completes and control
returns to B(), B's members are initialized, and then C's init block runs?

Ie, why doesn't the call to super constitute enough to run the init blocks?

class A { { System.out.println("A init") } }
class B extends A { { System.out.println("B init") } }
public class C extends B { { System.out.println("C init") }

public static void main(String[] args) {
C c = new C();
}
}
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Truitt:
...why doesn't the call to super constitute enough to run the init blocks? ...

I'm not sure I'm following. The initializer blocks do run.
[ August 25, 2008: Message edited by: marc weber ]
 
Ken Truitt
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ack well I put that hypothetical together based on a question on
the k&b cd test from the 1.5 book. In a similar situation (maybe somebody knows it--the classes are called Top, Middle, and Bottom) only the init
block from the class that is explicitly instantiated via the new keyword runs--according the the answer report.

You are saying that the call to super() WILL cause super's init blocks, if
any, to run at some point. That makes sense to me.

But it gets you to thinking--are there full-fledged instances of A and B on the heap without references (that we know about) to them? Is that what happens when the superclasses of a subclass that is explicitly instantiated are 'constructed' via calls to super?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Truitt:
... But it gets you to thinking--are there full-fledged instances of A and B on the heap without references (that we know about) to them? Is that what happens when the superclasses of a subclass that is explicitly instantiated are 'constructed' via calls to super?

When you say "extends," you are saying "I want a class like the superclass, but I want to extend it with additional (or overridden) functionality." The superclass acts as a base or foundation to build on.

When you create an instance of C, an instance of A is constructed. Then the instance of A is "extended" to construct an instance of B. Then the instance of B (which is also an instance of A) is "extended" to construct an instance of C.

So you might think of C as being like a building that has had additional wings added to it. The "original structure" (A) is still inside it.
[ August 25, 2008: Message edited by: marc weber ]
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
all the initializer blocks are transfered into the constructor in the order of their declaration. the general form of the constructor provided by the compiler is

constructor()
{
super();
intializer blocks and expressions in the order of their declaration;
constructor statements;
}

I hope this will clear your confusion....
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you might think of C as being like a building that has had additional wings added to it. The "original structure" (A) is still inside it.


Here the A,B objects created in the heap and C extending(inheriting) both of them directly/indirectly(A). And java knows how to refer to those objects internally from C object right ?
 
Ken Truitt
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clearing this up everybody. Only remaining question would
be where member vars are initialized in relation to init blocks--before
or after?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Truitt:
... where member vars are initialized in relation to init blocks--before or after?

In the order they appear in the code, as illustrated below...

OUTPUT:
Initializing int 1.
In initialization block 1.
Initializing int 2.
In initialization block 2.
Initializing int 3.
In constructor.

Note: It would be good practice to modify the above code by mixing in static variables and static blocks to demonstrate where these fit in. Then take it a step further by writing a similar class that extends this one. You might be surprised by the result.
[ August 26, 2008: Message edited by: marc weber ]
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
init block code will be copied to all the constructors just after call to super class constructor.

I wrote this code to test



and when i decompiled the generated class file it generated java file like this

 
Ken Truitt
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, both clear demonstrations of the sequence. It appears that init blocks and member vars are peers as far as initialization sequence goes. Surprising--I figured all inits would come before or after instance vars.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic