• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Order of Execution(Interesting)-:)

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
     
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Ranch Hand
    Posts: 66
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Vidya.

    Thats really a good link.

    Karuna.
     
    Girish Nagaraj
    Ranch Hand
    Posts: 153
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Vidya thanks for the link.....
     
    Girish Nagaraj
    Ranch Hand
    Posts: 153
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Vidya thanks for the link.-
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic