File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Order of Execution(Interesting)-:)

 
Girish Nagaraj
Ranch Hand
Posts: 153
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 153
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 11
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 63
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 13
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Vidya.

    Thats really a good link.

    Karuna.
     
    Girish Nagaraj
    Ranch Hand
    Posts: 153
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Vidya thanks for the link.....
     
    Girish Nagaraj
    Ranch Hand
    Posts: 153
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Vidya thanks for the link.-
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic