Phil
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]
Power from within.
Failed SCJP 2 times :-(
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).
Phil
Power from within.
Failed SCJP 2 times :-(
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.
Phil
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?
All code in my posts, unless a source is explicitly mentioned, is my own.
Power from within.
Failed SCJP 2 times :-(
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.
All code in my posts, unless a source is explicitly mentioned, is my own.
Power from within.
Failed SCJP 2 times :-(
Madura Shantha.
All code in my posts, unless a source is explicitly mentioned, is my own.
Power from within.
Failed SCJP 2 times :-(
All code in my posts, unless a source is explicitly mentioned, is my own.
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
|