• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

want to know the order of execution?

 
shreya prabhu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-------------------------------------------------------------
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic