Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook | Java 11 Cookbook
Rohit Ramachandran wrote: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?
Rohit Ramachandran wrote:
This prints 0. So the implicitly implemented constructor assigns a default value of 0.
When you instantiate a new Coffee object with the new operator, the Java virtual machine first will allocate (at least) enough space on the heap to hold all the instance variables declared in Coffee and its superclasses. Second, the virtual machine will initialize all the instance variables to their default initial values. Third, the virtual machine will invoke the <init> method in the Coffee class.
Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook | Java 11 Cookbook
mohamed sanaullah wrote:This link- Inheritance and initialization order - Should be useful. I really learnt a lot from this post!! Thanks Rohit for bringing it up
An excerpt from the article:
When you instantiate a new Coffee object with the new operator, the Java virtual machine first will allocate (at least) enough space on the heap to hold all the instance variables declared in Coffee and its superclasses. Second, the virtual machine will initialize all the instance variables to their default initial values. Third, the virtual machine will invoke the <init> method in the Coffee class.
So what I could make out from this is- The initialization of all the instance variables - is done by the VM and then the initializers and the constructors are invoked. Correct me if I interpreted it differently.
The complete article is also a good read!!
Rohit Ramachandran wrote:I suggest you read what I've written in my first post and answer accordingly.
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 van Hulst wrote:That's because you declared the initializer block before you declared the field. This has nothing to do with execution order. The Java Language requires you to have declared everything *before* it is referenced in an initializer block.
Rohit Ramachandran wrote: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-
Rohit Ramachandran wrote:
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.
Rohit Ramachandran wrote:It doesn't seem like it's the implicit constructor (in the absence of a user declared one) that initializes the values to zero.
Henry Wong wrote:
Stephan van Hulst wrote:That's because you declared the initializer block before you declared the field. This has nothing to do with execution order. The Java Language requires you to have declared everything *before* it is referenced in an initializer block.
Not true. Java allows the usage of an instance variable, that is forward referenced from the instance initializer.
Henry
Stephan van Hulst wrote:
Okay. So what's wrong with Rohit's code?
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
love demgracy, knowledge demogracy, open source and Java - OCPJP 76%
Imad Aydarooos wrote:Then for the first posted code the steps are :
2- the constructor reassign the value 0 to the member variable index
Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook | Java 11 Cookbook
Stephan van Hulst wrote:That's because you declared the initializer block before you declared the field. This has nothing to do with execution order. The Java Language requires you to have declared everything *before* it is referenced in an initializer block.
love demgracy, knowledge demogracy, open source and Java - OCPJP 76%
Imad Aydarooos wrote:I think its legal to declare initialization block before the declaration of the member variable
its accessing the member variable before its declaration
Rohit Ramachandran wrote:As far as I know, instance blocks are executed after all the superclass constructors have completed. But this seems to prove it wrong.
ocpjp 6.0 certified.
Abimaran Kugathasan wrote:This is the object creation order.
1. Set fields to default initial values (0, false, null)
2. Call the constructor for the object (but don't execute the body of the constructor yet)
3. Invoke the constructor of the superclass
4. Initialize fields using initializers and initialization blocks
5. Execute the body of the constructor
swaraj gupta wrote:Hello Abimaran,
what do you mean by initializers in 4th point..?
ocpjp 6.0 certified.
swaraj gupta wrote:
what do you mean by initializers in 4th point..?
Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook | Java 11 Cookbook
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
amit mandal wrote:
i think by initializers Abhimaran meant the explicit initializers like int i=10;
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abimaran Kugathasan wrote:
amit mandal wrote:
i think by initializers Abhimaran meant the explicit initializers like int i=10;
I'm searching this guy here & there. Do any one find him?
ocpjp 6.0 certified.
amit mandal wrote:
Oh my God not again :P
sorry buddy I once again miss-spelled your name :P hehehe
am now going to burn it in my brain : Abimaran hehehehe
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Stephan van Hulst wrote:
Imad Aydarooos wrote:I think its legal to declare initialization block before the declaration of the member variable
its accessing the member variable before its declaration
You are contradicting yourself, I'm afraid.
love demgracy, knowledge demogracy, open source and Java - OCPJP 76%
Don't get me started about those stupid light bulbs. |