• 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

Object Creation Steps

 
Ranch Hand
Posts: 193
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below are the object creation steps as I understand them. For now I don't want to consider any parent classes other than the Object class.

When the following code executes: these are the steps occurring in the JVM:

1. Allocate space
2. Assign any instance variables to zero initialization values.
3. Assign any instance variables to any explicit values.
4. Run any initialization blocks.
5. Run constructor
6. Call super(); ---in this case, Object's default constructor.
7. Complete constructor execution.


Is the above correct? Thank you.
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In this example, VehicleTest creates a new Car instance. Here's what happens in the JVM.

1. The Car constructor is called.
2. Before the Car's constructor code is ran Vehicle's constructor is called.
3. Before the Vehicle's constructor code is ran Object's constructor is called (runs up the inheritance tree).
4. Object's constructor code runs and completes (allocating the new object space).
5. Vehicle's instance variables are assigned their default values.
6. Vehicle's constructor code runs and completes.
7. Car's instance variables are assigned their default values.
8. Car's constructor code runs and completes.
9. Control is handed back to the main method.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Higgledy Smith wrote:
1. Allocate space
2. Assign any instance variables to zero initialization values.
3. Assign any instance variables to any explicit values.
4. Run any initialization blocks.
5. Run constructor
6. Call super(); ---in this case, Object's default constructor.
7. Complete constructor execution.
[/color]



I think, that point 4 is wrong. Initialization blocks can run before a constructor runs, or afterwards.
There is a difference between a static initialization block (runs on class load, before constructor runs) and a initialization block thats non-static (runs after the constructor and with each instantiation of this class).
 
Higgledy Smith
Ranch Hand
Posts: 193
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True, there is a difference between a static block and a non-static block. For sanity's sake, can you let me know where you read that non-static initialization blocks run AFTER the constructor?



sebastian tortschanoff wrote:

Higgledy Smith wrote:
1. Allocate space
2. Assign any instance variables to zero initialization values.
3. Assign any instance variables to any explicit values.
4. Run any initialization blocks.
5. Run constructor
6. Call super(); ---in this case, Object's default constructor.
7. Complete constructor execution.
[/color]



I think, that point 4 is wrong. Initialization blocks can run before a constructor runs, or afterwards.
There is a difference between a static initialization block (runs on class load, before constructor runs) and a initialization block thats non-static (runs after the constructor and with each instantiation of this class).

 
sebastian tortschanoff
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've got to be real: I've not read it, i've tested it while debugging this code:

This is the code from K&B Chapter 3 Question 6 a little bit enhanced:



The Ouput should something like this:

This runs only once!
Block is called every time by new instance.
0
Block is called every time by new instance.
0
Block is called every time by new instance.
0




When i debugged that code i've noticed, that the constructor



ran before the initialization block:



Just try it.
 
Higgledy Smith
Ranch Hand
Posts: 193
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is good stuff. I will try it. Thank you.


sebastian tortschanoff wrote:I've got to be real: I've not read it, i've tested it while debugging this code:

This is the code from K&B Chapter 3 Question 6 a little bit enhanced:



The Ouput should something like this:

This runs only once!
Block is called every time by new instance.
0
Block is called every time by new instance.
0
Block is called every time by new instance.
0




When i debugged that code i've noticed, that the constructor ran before the initialization block:



Just try it.

 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Higgledy Smith wrote:True, there is a difference between a static block and a non-static block. For sanity's sake, can you let me know where you read that non-static initialization blocks run AFTER the constructor?


Technically, to say that non-static initialization blocks run after the constructor is wrong. When you instantiate an object the instance initialization code is run in this order:

The appropriate constructor is called. If that constructor calls another constructor in the same class via a this(...) call, then call that constructor, and so on. Eventually you will reach a constructor that doesn't have a this(...) call as its first statement. In that case, the first statement will be either an explicit or implicit call to a superclass constructor Call the superclass constructor, and complete instance initialization for the superclass following this same process. When the superclass constructor returns, then run all instance initializers and instance initialization blocks in the order in which they are declared in the class code. Then, execute the rest of this constructor.

It might seem a little convoluted, but when you get it you won't have any more doubts.
 
sebastian tortschanoff
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ruben,

thank you for explaining. Unfortunately i suggested wrong.
It's truly a good thing to know exactly how (in wich order) constructors are called.

I understand, that at first the constructor is called with "this":



the initializer block runs (not the static one) and finally there is the return to the constructor which takes an int as argument.

Phew! Im sure i won't get in trouble with constructors, when i'm doing the exam


Thanks a lot for explaining.

 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sebastian tortschanoff wrote:Hi Ruben,

thank you for explaining. Unfortunately i suggested wrong.
It's truly a good thing to know exactly how (in wich order) constructors are called.

I understand, that at first the constructor is called with "this":



the initializer block runs (not the static one) and finally there is the return to the constructor which takes an int as argument.

Phew! Im sure i won't get in trouble with constructors, when i'm doing the exam


Thanks a lot for explaining.


Hi Sebastian,

If a constructor has a call to this(...) or super(...), then the compiler doesn't insert a call to super(). Ultimately one of the constructors in every chain must call a superclass constructor. So in the case above, your constructor calls this(2), which calls the constructor which takes an int argument. Then, if that constructor calls a superclass constructor, that constructor is called, and once the superclass constructor returns, then you run the instance initializer and instance initialization blocks. Then finish the rest of the constructor, and then return to the first constructor and finish its code.

The main idea is that you can only run initialization code for a given class when the constructor for the superclass has returned (not before.)

Once you understand how it works, you won't have any trouble. Let me know if you have any other questions. The best way to troubleshoot this is to write an example program and have each constructor output something to the console. That way you can track the order in which code is run.
 
sebastian tortschanoff
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ruben,

so far so good. Actually i have two constructors. The one calls this, wich calls the constructor wich takes an int as argument. But where the heck is the call to super(); (Superclass Object)?


 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sebastian,

Twisty (int i){
super();
index = i;
}

Twisty (){
this(2);
}

look at this code. you can clearly see the call to super class constructor. Believe me, this is exactly similar to your code.
what happens here is, if you don't add the super() call, java adds it for you. if you add this() or super call, then java doesn't add the default super call. note that the default super call is always with no arguments. in this way. if your super class doesn't have a constructor with no arguments, this gives a compile error, because java always create super call with no args and the super class doesn't have it.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sebastian,

I think Madura explained everything well. Every constructor (no exceptions) must have either a call to super(...) or this(...) as its first statement (notice that ... means any, including no arguments.) If you don't have one of these calls explicitly coded in a constructor, then the compiler will insert a call to super() as the first statement.
 
sebastian tortschanoff
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep. Thanks a lot.
This forum is worth diamonds...

best regards
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome, Sebastian. Yes, this forum is a great resource, isn't it?
 
You've gotta fight it! Don't give in! Read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic