aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object Creation Steps Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object Creation Steps" Watch "Object Creation Steps" New topic
Author

Object Creation Steps

Higgledy Smith
Ranch Hand

Joined: Mar 07, 2006
Posts: 192
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.
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192


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.
sebastian tortschanoff
Ranch Hand

Joined: Mar 19, 2009
Posts: 68
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).


Power from within.

Failed SCJP 2 times :-(
Higgledy Smith
Ranch Hand

Joined: Mar 07, 2006
Posts: 192
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

Joined: Mar 19, 2009
Posts: 68
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

Joined: Mar 07, 2006
Posts: 192
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.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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.


All code in my posts, unless a source is explicitly mentioned, is my own.
sebastian tortschanoff
Ranch Hand

Joined: Mar 19, 2009
Posts: 68
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

Joined: Dec 16, 2008
Posts: 1032
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

Joined: Mar 19, 2009
Posts: 68
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)?


madura shantha
Greenhorn

Joined: Jun 04, 2008
Posts: 8
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.


Madura Shantha.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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

Joined: Mar 19, 2009
Posts: 68
Yep. Thanks a lot.
This forum is worth diamonds...

best regards
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
You're welcome, Sebastian. Yes, this forum is a great resource, isn't it?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object Creation Steps
 
Similar Threads
initilizer expressions
Instantiation/Forward referencing
this in new class construction
Inheritance
this(i)