aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Order of Execution(Interesting)-:) 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 "Order of Execution(Interesting)-:)" Watch "Order of Execution(Interesting)-:)" New topic
Author

Order of Execution(Interesting)-:)

Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
Have a look at this code.........

public class Test35 {

int k;

{
k = 4;
System.out.println("In Initializer block");
System.out.println("this.k = " + this.k);
}

Test35() {

System.out.println("In constructor");
}

public static void main(String[] args) {

new Test35();
}
}

Output is:
In Initializer block
this.k = 4
In constructor

Object of type Test35 is Instantiated during constructor call.
So I thought Initializer block is called after constructor call but the output is other way, WHY???

Or is object Instantiated before Initializer block is called???
mohamed ewis
Greenhorn

Joined: Feb 05, 2006
Posts: 18
hi
the order of execuation is

1- static inializers
2- super constractor
3- non static inializers allocated in the memory
4-allocate constracuctors in memory
Shaliey Gowtham
Ranch Hand

Joined: Mar 20, 2006
Posts: 104
The order of execution is
When the class loaded, Static blocks are called once in the order they are declared.
When a new object is created then initializer blocks are called in the order they are declared and then the constructor is exceuted.

In constructor execution consider,

In this code when new B(10) is executed then constructor B(int i) is invoked then the variable i is binded to value 10.
Then the super class contructor A() is called.
It prints "Inside Super";
Then B(int i) executes and prints "Inside subClass"
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
It did not answer my Question.....

My Question is When exactly instance of object is created.Is it before call to non static initializes???
Shaliey Gowtham
Ranch Hand

Joined: Mar 20, 2006
Posts: 104
When ever the object is created using new
  • Memory is allocated and default initialisation occurs.
  • Non-static initializers are executed in order.
  • Constructor is executed


  • To your question the object is created before the non-static initializers are executed[i.e once the super class constuctors are run and memory is allocated, the object is created but doesn't have a state]
    Correct me if wrong
    Vidhya Praba
    Greenhorn

    Joined: Apr 13, 2006
    Posts: 11
    Hi,

    Check out Corey's blog reg. this in the following link...

    http://radio.javaranch.com/corey/2004/04/19/1082390724000.html

    It quotes the relevant section of the JLS as well. I think this answers your question...

    Vidhya.
    M Mistroni
    Ranch Hand

    Joined: Oct 03, 2005
    Posts: 62
    Hello,
    i have this sample then.
    if static initializers are created when class is loaded, why then Parent static initializer gets executed before Child static initializer? because CHild IS-A parent? or because it cames earlier in the order?
    here's sample

    class Parent
    {
    int a = getA();


    public Parent()
    {
    System.out.println("Parent Class Constructor");
    }

    static {
    System.out.println("Parent Class Instance Initializer");
    }




    private int getA()
    {
    System.out.println("Parent Class Member Initializer");
    return 0;
    }
    }

    public class Child extends Parent
    {


    int b = getB();

    static {

    System.out.println("Child Class Instance Initializer");
    }


    //

    public Child()
    {
    System.out.println("Child Class Constructor");
    }

    private int getB()
    {
    System.out.println("Child Class Member Initializer");
    return 0;
    }

    public static void main(String[] args)
    {
    Child c = new Child();
    System.out.println("Done" + c.b);
    }
    }


    thanx abd regards
    marco
    Karuna Kethineedi
    Greenhorn

    Joined: Apr 25, 2006
    Posts: 13
    Thanks Vidya.

    Thats really a good link.

    Karuna.


    Arise, awake, stop not till the goal is reached !! <br /> <br /> Swami Vivekanada
    Girish Nagaraj
    Ranch Hand

    Joined: Apr 19, 2006
    Posts: 153
    Vidya thanks for the link.....
    Girish Nagaraj
    Ranch Hand

    Joined: Apr 19, 2006
    Posts: 153
    Vidya thanks for the link.-
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Order of Execution(Interesting)-:)
     
    Similar Threads
    Static Blocks and Instance Initializers (on certification exam?)
    Initialiser Blocks
    Initializers
    Inherited Constructor invocation
    initialization sequence - what is done first?