Reputation: 1537
I have two classes Parent and Child
public class Parent {
public Parent() {
System.out.println("Parent Constructor");
}
static {
System.out.println("Parent static block");
}
{
System.out.println("Parent initialisation block");
}
}
public class Child extends Parent {
{
System.out.println("Child initialisation block");
}
static {
System.out.println("Child static block");
}
public Child() {
System.out.println("Child Constructor");
}
public static void main(String[] args) {
new Child();
}
}
The output of the above code will be
Parent static block
Child static block
Parent initialization block
Parent Constructor
Child initialization block
Child Constructor
Why does Java execute the code in that order? What are the rules that determine the execution order?
Upvotes: 100
Views: 98640
Reputation: 1
Learn more with this video: Java Tutorial by Rajan Jain : Static Init Block and Init Block in java Video14 - YouTube
Upvotes: 0
Reputation: 32083
I learn visually, so here's a visual representation of order, as a SSCCE:
public class Example {
static {
step(1);
}
public static int step_2 = step(2);
public int step_8 = step(8);
public Example(int unused) {
super();
step(10);
}
{
step(9);
}
// Just for demonstration purposes:
public static int step(int step) {
System.out.println("Step " + step);
return step;
}
}
public class ExampleSubclass extends Example {
{
step(11);
}
public static int step_3 = step(3);
public int step_12 = step(12);
static {
step(4);
}
public ExampleSubclass(int unused) {
super(step(7));
step(13);
}
public static void main(String[] args) {
step(5);
new ExampleSubclass(step(6));
step(14);
}
}
This prints:
Step 1
Step 2
Step 3
Step 4
Step 5
Step 6
Step 7
Step 8
Step 9
Step 10
Step 11
Step 12
Step 13
Step 14
Keep in mind that the order of the static
parts matters; look back at the difference between the order of Example
's static
stuff and ExampleSubclass
's.
Also note that the instance initialization block is always executed immediately after the super()
call in the constructor (even if that call is implied/omitted), no matter the order. However, order does matter between an initialization block and a field initializer.
Upvotes: 78
Reputation: 199
(In your code the parent static blocks will be executed first and then the child class static blocks.)
In your code when you create a Child object:
Upvotes: 9
Reputation: 82
Here is what I found while preparing for a certification.
While we run a class, first static blocks/ static variable initialisation happens. If multiple static blocks are there, it will execute it in the order in which it appears,
Then it will execute init blocks/ instance variable initialisation.If multiple init blocks/ variable initialisation are there, it will execute it in the order in which it appears,
Afterwards it will look into the constructor.
Upvotes: 2
Reputation: 511
Just wanted to share my findings. I read in one of the answers on another thread that static blocks are executed first before static fields which is not correct. It depends on which comes first, static field or static block. Have a look at below code. It will try to put things in perspective.
It comes back to main method.
class TestLab {
static int method(String a) {
System.out.println("in static method of TestLab" + " Coming from " + a);
System.out.println("b is " + b);
return 6;
}
static int a = method("Line 11");
static int b = 7;
TestLab() {
System.out.println("Inside test lab constructor");
}
static {
System.out.println("In static block of TestLab");
}
}
public class Test1 {
public static void main(String[] args) {
System.out.println("inside main method of Test 1");
int a = TestLab.method("Line 26");
}
// static Test ref=new Test();
Test1() {
System.out.println("Default Constructor of Test1");
}
{
System.out.println("In instance block of Test1");
}
static int d = TestLab.method("Line 37");
static int e = methodOfTest1();
static {
System.out.println("In Static Block of Test1");
}
static int methodOfTest1() {
System.out.println("inside static method:mehtodOfTest1()");
return 3;
}
}
Here is the output:
in static method of TestLab Coming from Line 11
b is 0
In static block of TestLab
in static method of TestLab Coming from Line 37
b is 7
inside static method:mehtodOfTest1()
In Static Block of Test1
inside main method of Test 1
in static method of TestLab Coming from Line 26
b is 7
Upvotes: 4
Reputation: 102
control flow is-
static block -> Initialization block -> and finally Constructor.
static block -> This static block will be get executed only once when the control come to the class.(JVM Load this class)
Initialization block -> This Initialization block will be get executed whenever a new object Created for the Class (It will be executed from second statement of the Constructor then following constructor statements- remember First statement of the Constructor will be Super()/this())
Constructor -> This will be get whenever a new object is created.
Upvotes: 1
Reputation: 27496
There are several rules in play
super();
before executing it's own constructor. Initialization block comes into play even before the constructor call, so that's why it is called first. So now your parent is created and the program can continue creating child class which will undergo the same process.Explanations:
- Static block of parent is executed first because it is loaded first and static blocks are called when the class is loaded.
Upvotes: 61
Reputation: 61
It would be very helpful to ckeck out the object construction process with a step by step debuger, having a view in which you can see how your object is goning through the phases. I found this very useful for clearing the perspective from a higher point of view. Eclipse can help you with this with it's debugger step into function.
Upvotes: 3
Reputation: 881
Static block gets executed when a class is loaded into JVM. While init block gets copied into the Constructor whose object will be created and runs before creation of object.
Upvotes: 1
Reputation: 1552
First - run child class only (comment the extend clause) to see the simple flow.
second - go to Static block vs. initializer block in Java? & read the accepted answer over there.
Edit:
Upvotes: 9
Reputation: 581
Static block in java is executed before main method. If we declare a Static block in java class it is executed when class loads. This is initialize with the static variables. It is mostly used in JDBC. Static block in java is executed every time when a class loads. This is also known as Static initialization block. Static block in java initializes when class load into memory , it means when JVM read the byte code. Initialization can be anything; it can be variable initialization or anything else which should be shared by all objects of that class. Static block is a normal block of code enclosed in braces { } and is preceded by static keyword.
so static block executed first.
Instance Initialization Blocks: Runs every time when the instance of the class is created.
so next Initialization block executed when instance of the class is created.
then Constructor executed
Upvotes: 5