• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the order of intialization in this code?  RSS feed

 
Radulescu Iulia
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In the following Java code, the Insect class is inherited by the Beetle class.



The output is somehow comfusing.

    static Insect.x1 initialized

    static Beetle.x2 initialized

    Beetle constructor

    i = 9, j = 0

    Beetle.k initialized

    k = 47 j = 39

What is the order of initialization in this particular example?

Why is x2 initialized immediatly after x1? And why is the first line of the main method (System.out.println("Beetle constructor");), executed after x1 and x2 intializaton. So comfusing.

I know that the derived class constructor automatically calls the base class constructor (unless it has parameters and you'd use the super keyword). I know that variables are initialized before the constructor, and that static variables are initialized before other variables.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because your main() method is in the Beetle class the JVM has to load Beetle before it can do anything.

Since Beetle extends Insect, it has to load Insect first.

So the order is, load Insect (executing any static stuff), load Beetle (executing any static stuff), run main().
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how it gets executed:

1. static fields declared in class are initialized to default values or user assigned values.
2. static initializer blocks get executed in order they are defined.

Note: Before control comes to main method above two steps of super class is executed first then above two steps of subclass.
Now Control comes to main method. If object of sub class is created then only

3. initializes instance variables declared in class to default values or user assigned values.
4. executes initialization blocks in order they are defined.
5. default or parameterized constructor is called which depend on how object is created.

Note: Above 3 steps of super class is executed first then above 3 steps of subclass.

This is how it would look like
ExecutionProcess.png
[Thumbnail for ExecutionProcess.png]
 
Radulescu Iulia
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh Patekar wrote:This is how it gets executed:

1. static fields declared in class are initialized to default values or user assigned values.
2. static initializer blocks get executed in order they are defined.

Note: Before control comes to main method above two steps of super class is executed first then above two steps of subclass.
Now Control comes to main method. If object of sub class is created then only

3. initializes instance variables declared in class to default values or user assigned values.
4. executes initialization blocks in order they are defined.
5. default or parameterized constructor is called which depend on how object is created.

Note: Above 3 steps of super class is executed first then above 3 steps of subclass.

This is how it would look like


So I could say, as a general rule, that:

1. Static members in the base class are initialized first,
2. Static members in the derived class are initialized next,
3. Base class non-static members (primitives and references) are initialized
4. Base class constructor gets called
5. Derived class non-static members are initialized
6. Finally I have everything I need to create the object, so the derived class constructor gets called.

Am I right?
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think members also includes methods so better use field word ok.

1. static fields in the base class are initialized first,
2. static initializer of the the base class gets executed.
3. static fields in the derived class are initialized first.
4. static initializer of the the derived class gets executed.
5. Base class non-static fields means instance variables( It could be primitives and references) are initialized.
6. Initialization block of base class gets executed.
7. Base class constructor gets called.
8. Derived class non-static fields means instance variables( It could be primitives and references) are initialized.
9. Initialization block of derived class gets executed.
10. Derived class constructor gets called.

I hope you know what is static initializer and initialization block.

Finally I have everything I need to create the object, so the derived class constructor gets called.

No, When you create an object of derived class then constructor of base class gets executed first, then derived class's constructor.

Suppose you have static fields, static initializer, instance variable, initialization block and non-arg constructor in base class as well as in derived class. Both of them have these things of their own.

scenario 1:
  • Suppose you neither create an object of derived class nor base class then when you run program then first 4 steps gets executed in give order next steps will never get executed because they are triggered only when you either create an object of derived class or base class.

  • scenario 2:
  • Suppose you create an object of derived class. When you run program steps 1 to 10 gets executed in given order.


  • scenario 3:
  • Suppose you create an object of base class only. When you run program steps 1 to 7 gets executed only rest 8 to 10 will not because they are  triggered only when you create an object of derived class.


  • These things can easily be understood, If you create such program and try printing messages in each block so you will get that very easily.
    Hope you got that
     
    Campbell Ritchie
    Marshal
    Posts: 55789
    164
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganesh Patekar wrote:I think members also includes methods  . . .
    The definition is in the Java® Language Specification (=JLS). Yes, it includes methods. I shall let you read it and find what else counts as a member. There is probably a better JLS section defining members. Can't seem to find it at the moment.
    Did you get the order of execution from the JLS? It is specified by the JLS, but I you would have to look through the index to find which section.
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes read rest of things which are also termed as members of a class. Thank you 
    Did you get the order of execution from the JLS? It is specified by the JLS,
    No, I tried and observed it practically. If It is also given in JLS please let me know whenever you come across to that.
     
    Campbell Ritchie
    Marshal
    Posts: 55789
    164
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There are two JLS sections which might help: 1 2.
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    They gave evrything how execution occurs and all. I dunno How I missed ch 12 which tells from first step to last step of execution. The problem because I tried to read information from JLS according to Java oracle tutorial, I mean in general way which starts with what is class and variable etc etc. But It's good to read JLS bit confusing but once we understand their langauge and how It is organised then we don't need any other resources at least to grasp basic fundamentals of Java.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!