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

posted Mar 23, 2009 08:43:17

0

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?

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

posted Mar 23, 2009 09:05:59

0

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

posted Mar 23, 2009 09:10:12

0

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:

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

posted Mar 23, 2009 12:49:59

0

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

posted Mar 23, 2009 16:29:02

0

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 &amp;quot;this&amp;quot;:

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

posted Mar 24, 2009 01:50:18

0

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)?

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

posted Mar 24, 2009 10:11:48

0

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

posted Mar 24, 2009 10:30:22

0

Yep. Thanks a lot. This forum is worth diamonds...

best regards

Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032

posted Mar 24, 2009 15:56:43

0

You're welcome, Sebastian. Yes, this forum is a great resource, isn't it?