Forums Register Login

want to know the order of execution?

+Pie Number of slices to send: Send
i am confused regarding order of execution?
is it:
static initializers and static variables first and then instance initializers and instance variables?please clear my doubt or is it in the order of declaration?
+Pie Number of slices to send: Send
 

Originally posted by archana prabhu:
i am confused regarding order of execution?
is it:
static initializers and static variables first and then instance initializers and instance variables?please clear my doubt or is it in the order of declaration?



The static variable initializers and static initializer blocks in a class definition are initialized when a class is initialized.

This is from the Java Language Specification 12.4.2.

Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block, except that final class variables and fields of interfaces whose values are compile-time constants are initialized first

This is step 9 in initializing a class.

The others steps aren't relevant to your question.

The instance variable initializers and instance initializer blocks are only executed when an instance is created.

All superclasses will be initialized first, then the instance variable initializers and instance initializer blocks are executed from top to bottom.

Then the rest of the constructor executes.
[ March 19, 2007: Message edited by: Keith Lynn ]
+Pie Number of slices to send: Send
 

-------------------------------------------------------------
i am confused regarding order of execution?
is it:
static initializers and static variables first and then instance initializers and instance variables?please clear my doubt or is it in the order of declaration?
--------------------------------------------------------------



Consider the following program

class Parent{

static int statVar=10;
int initVar=100;

static {
System.out.println("static1 - parent "+statVar);
}

{
System.out.println("instance init1 - parent "+initVar);
}

Parent(){
System.out.println("Constructor - parent");
}

static {
System.out.println("static2 - parent");
}

{
System.out.println("instance init2 - parent");
}


}

public class Child extends Parent{

static {
System.out.println("static1 - Child");
}

{
System.out.println("instance init1 - child");
}

Child(){
System.out.println("Constructor - child");
}

static {
System.out.println("static2 - child");
}

{
System.out.println("instance init2 - child");
}


public static void main(String[] args) {
new Child();
}
}


The output will be

static1 - parent 10
static2 - parent
static1 - Child
static2 - child
instance init1 - parent 100
instance init2 - parent
Constructor - parent
instance init1 - child
instance init2 - child
Constructor - child

We can have conclusion about order of execution like following

1. static variables and then Static initializers throghout the inheritance hienrarchy as per coding order.
2. Following will happen for individual classes from the top of the hierarchy
a. instance variables and then instance inializers
b. constructor
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 791 times.
Similar Threads
which comes first?
static variables and blocks
Initialization
interface question
order of execution in java
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:36:28.