wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes want to know the order of execution? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "want to know the order of execution?" Watch "want to know the order of execution?" New topic
Author

want to know the order of execution?

shreya prabhu
Ranch Hand

Joined: Feb 10, 2007
Posts: 31
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?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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 ]
JB Ramesh
Greenhorn

Joined: Jan 09, 2007
Posts: 20
-------------------------------------------------------------
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: want to know the order of execution?
 
Similar Threads
static variables and blocks
which comes first?
Initialization
interface question
order of execution in java