Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!

Rohit Ramachandran

Ranch Hand
+ Follow
since Oct 05, 2010
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Rohit Ramachandran

It'll come in a day or two I think. I did the same for scjp and I co-ordinated with the official and got it in like 3 days.
Great score.
7 years ago
What do you guys suggest on taking up the Programmer certified professional upgrade exam?

7 years ago
Cleared with a 70%. Not satisfied, not disappointed either. Anyway, will train harder for the next level. What do you guys suggest? SCWCD or SCJD?
7 years ago
Which code doesn't compile? There're quite a few extracts above.
It doesn't seem like it's the implicit constructor (in the absence of a user declared one) that initializes the values to zero. Because of the inconsistency in the order in which it is run.
Right Right, completely get it now. Thanks Stephan van Hulst.
Right Right, completely get it now. Thanks Stephan van Hulst

Stephan van Hulst wrote:1. Memory for the entire object is allocated
2. All fields (including fields of superclasses) are set to default values: booleans are false, ints are 0, references are null, etc.

3. For each superclass of the object, starting with Object and working down:
a. fields are initialized if they have explicit initializers (e.g. private int = 3);
b. initializer blocks are executed;
c. constructor is executed;

Let's say we have a Tiger, which is an Animal, which is an Object:

First we allocate memory for the entire object, including all the fields of Tiger, Animal and Object. Then all these fields are set to 'zero'.
Now, all the fields of Object are initialized with their given values. Then the initializer blocks of Object are executed. Then the remaining statements in Object's constructor are run.
Then the same thing happens for Animal, first its fields are set explicitly, then the initializers are run, and finally the constructor is completed.
Last but not least, the exact procedure happens for Tiger.

Stephan if that's the case this code should compile, but it doesn't. It says "Illegal forward reference."

The problem is that there's no consistency. When you implement the constructor, then it is run after the instance block but if you don't implement the constructor, then the instance block is run after the constructor, so it seems.

What I can deduce from this is that, the order is-

1. Default constructor
2. super();
3. Instance block
4. Implemented constructor

But if you implement a constructor, there is no default constructor hence super(), instance block and implemented constructor.

I suggest you read what I've written in my first post and answer accordingly.

This prints 0. So the implicitly implemented constructor assigns a default value of 0.
Dude the code there, shouldn't a constructor be inserted implicitly that assigns index to zero?

Order of execution-

Super constructors, instance blocks, constructors right?

Therefore, first index=1, then index=0.

Therefore dd[0][1] should be the one it points to, but dd[1][2] is the one it points to. When I uncomment the constructor implementation, then it points correctly to dd[0][1] but shouldn't it point to it anyway considering the implicit declaration of the constructor by the compiler?
As far as I know, instance blocks are executed after all the superclass constructors have completed. But this seems to prove it wrong.

Output- 4

When the constructor in the comments are uncommented, the output is 8. Shouldn't it be eight anyway? The constructor assigns a default value of 0, which is run after the instance block thereby overwriting the value of index.