• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Initialisation blocks

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there all

Just revising for the SCJP and come accross a question about initialisation blocks. I have copied the following code from the SCJP Sierra bible making slight changes:

class Bird {
{System.out.println("1");}
public Bird() {
System.out.println("2");
}
}//End Bird

class Raptor extends Bird {
static {System.out.println("3");}
public Raptor() {
System.out.println("4");
}
{System.out.println("5");}
static {System.out.println("6");
}
}//End Raptor

public class Hawk extends Raptor {

public static void main(String[]args) {
System.out.println("7");
new Hawk();
System.out.println("8");
}
public Hawk() {
}
}//End Hawk

When I answered the question I was under the impression that the instance initialisation blocks ran after the super constructor ran or is just the call to the super()? When I ran the code the instance initialisation block runs first and then the Bird() constructor although this sounds correct??

This is the output:

3
6
7
1
2
5
4
8

Is the order as follows:

1) Static init blocks - as class is loading from main()
2) Initialisation blocks - as instance created
3) Super Constructor run

I'm confused because of the fact there are 3 classes??? Can anyone please clarify asap please?

Please help Obi-Wan your my only hope!

Many thanks in advance

Wayne

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you have given the answer.

1) Static init blocks - as class is loading from main()
2) Initialisation blocks - as instance created
3) Super Constructor run

When the class is loaded, it initialises the static blocks.
When the object is constructed, it initialises the instance block and then the constructor.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wayne!

The hierarchy is as follows:
Bird
Raptor extends Bird
Hawk extends Raptor

1. static initializer blocks run first, starting on top of the hierarchy (if any), order is important within a class. -> Output 3 and 6

2. Output 7 is obvious...

3. Afterwards, starting again on top of the hierarchy,
* first the instance block
* then the constructor and
* then it returns the control flow to the calling (sub-)class
This gives: 1 2 5 4

With other words, every constructor
* either calls the super-constructor
* or creates an instance - and therefore executes the instance initializer block if any - before it executes commands in the constructor!

This means, that if there is a super-class, then the constructor
* first calls the super-constructor and
* when control returns, it calls the instance initializer block and
* then the commands within the constructor.

Hope it's not too confusing...

I modified your example a little bit to show also other aspects of the problem:


This gives:
bird static
raptor static 1
raptor static 2
hawk static
hawk before instantiation
bird instance
bird constructor
raptor instance
raptor constructor
hawk instance
hawk constructor
hawk after instantiation

Cheers,
Robert
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

a complete example including variable initialisation can be found here.
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wayne,
Just remember these three points

1. First the static blocks gets executed only once that is when the class is loaded.Hierarachy will be from super class to sub class.

2.Initialization block runs once every time when the new instance is created.

3.Finally the constructors run that is from super class constructor to sub class constructor.

Remember the above three points and analyze your code you will get the concept.

Regards,
Nik
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nik Arora:
2.Initialization block runs once every time when the new instance is created.

3.Finally the constructors run that is from super class constructor to sub class constructor.

Not completely correct. Initialization blocks and constructors are not different steps.

- Initialization blocks from superclass
- Constructor from superclass
- Initialization blocks from subclass
- Constructor from subclass
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manfred Klug:
Not completely correct. Initialization blocks and constructors are not different steps.

- Initialization blocks from superclass
- Constructor from superclass
- Initialization blocks from subclass
- Constructor from subclass



I meant same but it was not properly explained.Good that you explained properly Manfred

Regards,
Nik
 
Wayne Pegg
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone

Many thanks. Makes sense now! Very much appreciated and thanks for your explanations and time.

Wayne
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well!!!I have a doubt on the above concept of initialisation please if anyone could clear it......

First of all link shown by Manfred,there i found example which i tried by modifying a bit...as shown below.....

why here method gets invoked than only static blocks get executed?



the output is as below

Testtry
Invoking Derived constructor...
ok
Base static initialization Block 1.
Base initializing static variable 1.
Base static initialization Block 2.
Base initializing static variable 2.
Derived static initialization Block 1.
Derived initializing static variable 1.
Derived static initialization Block 2.
Derived initializing static variable 2.
Base instance initialization Block 1.
Base initializing instance variable 1.
Base instance initialization Block 2.
Base initializing instance variable 2.
Base constructor body.
Derived instance initialization Block 1.
Derived initializing instance variable 1.
Derived instance initialization Block 2.
Derived initializing instance variable 2.
Derived constructor body.

But later in the example shown



output:
bird static
raptor static 1
raptor static 2
hawk static
hawk before instantiation
bird instance
bird constructor
raptor instance
raptor constructor
hawk instance
hawk constructor
hawk after instantiation

But later
the output varies,as inheritance is implemented here
.........
now i am gonna totally confused with answer,please if anyone could explain me......
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi dhwani,

could you formulate your doubt in one sentence? Currently I'm not sure what your problem is.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manfred!!

Actualy my doubt is ..In the first example which i have shown the class with main method Testtry does not extend any other class...so its static block gets executed first than the methods disp get executed later were it finds instance of class Derived than only...static blocks and initialisation blocks get executed........

Later in the second example class Hawk with main method extends Raptor so the super class static block get executed..than initialisations block and so on.....

But as per i understand it is mentioned that as class loads all static blocks in program get executed before the program jumps to the main method,and also initialisation blocks get executed later the statements in main method get invoked......

but in the above two programs,it is shows that if inheritance is implemented ie if class extends another class than only static blocks in super class get executed,otherwise if inheritance is not implemented in a class all the static blocks of this class get executed than methods in main method get executed,later if it finds instance of any class than that classes staic blocks get executed...........

please if anyone could explain me......what is the reason for this......it will kinda of you...
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi dhwani,

The static initialization is executed when the class is loaded, and a class is only loaded when it is needed.

In the first example, the JVM loads and initializes the Testtry class. But since it doesn't extend any class, no further initialization takes place. When you instantiate the derived class, it is loaded and initialized (static and instance).

In the second example, the Hawk class extends another class. As result, the JVM loads and initializes the parent classes too, and when you instantiate the Hawk class, the static initialization is already done.

I hope I got your Question.
[ July 27, 2007: Message edited by: Manfred Klug ]
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Manfred!!!Thanks...now my doubt is cleared.....
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great explanation Manfred!
 
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic