aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes init blocks and constructor chain Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "init blocks and constructor chain" Watch "init blocks and constructor chain" New topic
Author

init blocks and constructor chain

Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
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();
}
}


SCJP 88% | SCWCD 84%
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 03, 2008
Posts: 9280
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

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 ?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Mar 15, 2008
Posts: 210

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



SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: init blocks and constructor chain
 
Similar Threads
Constructor Behaviour
some simple Q....
Initialization
static initialization block
Call a method in super's super class??