| Author |
Instance blocks.
|
Rohit Ramachandran
Ranch Hand
Joined: Oct 05, 2010
Posts: 102
|
|
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.
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3053
|
|
Initialization blocks are executed after super class constructors. What have you done to prove this wrong?
Twisty() is not a super class constructor.
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2928
|
|
|
All classes extend the Object class.
|
Mohamed Sanaulla | My Blog
|
 |
Rohit Ramachandran
Ranch Hand
Joined: Oct 05, 2010
Posts: 102
|
|
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?
|
 |
swaraj gupta
Ranch Hand
Joined: Oct 22, 2010
Posts: 181
|
|
Mr. Ramchandran...,
This is how compiler provides the default constructor for a class..
which is totally different from what are you thinking. It does nothing except calling a super class constructor....
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
Ranch Hand
Joined: Oct 05, 2010
Posts: 102
|
|
This prints 0. So the implicitly implemented constructor assigns a default value of 0.
|
 |
swaraj gupta
Ranch Hand
Joined: Oct 22, 2010
Posts: 181
|
|
This is because if you do not explicitly assign values to instance variables, they get implicitly initialized by their default values when a new instance of their class is created. Default constructor does nothing in it....
Rohit Ramachandran wrote:
This prints 0. So the implicitly implemented constructor assigns a default value of 0.
|
 |
Rohit Ramachandran
Ranch Hand
Joined: Oct 05, 2010
Posts: 102
|
|
|
I suggest you read what I've written in my first post and answer accordingly.
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2928
|
|
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!!
|
 |
swaraj gupta
Ranch Hand
Joined: Oct 22, 2010
Posts: 181
|
|
sir, there is nothing wrong in it.
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!!
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3053
|
|
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.
|
 |
swaraj gupta
Ranch Hand
Joined: Oct 22, 2010
Posts: 181
|
|
my answer was according to your 3rd post. A constructor never implicitly initializes the instance variables. JVM does it internally (not through constructors) if you do not explicitly initialize them..
Rohit Ramachandran wrote:I suggest you read what I've written in my first post and answer accordingly.
|
 |
Rohit Ramachandran
Ranch Hand
Joined: Oct 05, 2010
Posts: 102
|
|
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.
Weird.
|
 |
Rohit Ramachandran
Ranch Hand
Joined: Oct 05, 2010
Posts: 102
|
|
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."
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3053
|
|
|
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
Ranch Hand
Joined: Oct 05, 2010
Posts: 102
|
|
|
Right Right, completely get it now. Thanks Stephan van Hulst
|
 |
Rohit Ramachandran
Ranch Hand
Joined: Oct 05, 2010
Posts: 102
|
|
|
Right Right, completely get it now. Thanks Stephan van Hulst.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
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-
There is no reason to deduce -- the order is defined by the JLS....
1. Compile time constants -- not really on the order list, but I like to list it first as constants are common.
2. The super part pf the constructor, but not the main body of the constructor.
3. Instance initializers and initialization of instance variables in the order they appear in code.
4. The rest of the constructor.
Henry
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
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.
Your no-arg constructor is different than the default constructor, that is inserted with no constructors in the class.
Your no-arg constructor has an assignment that sets the index to zero. The implicit default constructor doesn't do that. So, when you comment out your constructor, it is replaced with a constructor that does something different.
Henry
|
 |
Rohit Ramachandran
Ranch Hand
Joined: Oct 05, 2010
Posts: 102
|
|
|
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.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
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.
It isn't. Instance variables have a default value assigned sometime earlier than any instance variable init, instance initiailzer, super constructor, or constructor. I am guessing as early as part of the memory allocation.
It is not assigned the default value by the constructor -- implicit or not.
Henry
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3053
|
|
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
Okay. So what's wrong with Rohit's code?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Stephan van Hulst wrote:
Okay. So what's wrong with Rohit's code?
Nothing. It compiles fine.
As for why the answer is different, with and without his constructor, read a few posts back. His no-arg constructor and is not the same as the default constructor.
Henry
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3053
|
|
|
Doesn't compile for me...
|
 |
Rohit Ramachandran
Ranch Hand
Joined: Oct 05, 2010
Posts: 102
|
|
|
Which code doesn't compile? There're quite a few extracts above.
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
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
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Imad Aydarooos
Ranch Hand
Joined: Nov 02, 2010
Posts: 87
|
|
Then for the first posted code the steps are :
1- index the member variable gets a value 1 from the initialization block
2- the constructor reassign the value 0 to the member variable index
|
love demgracy, knowledge demogracy, open source and Java - OCPJP 76%
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2928
|
|
Imad Aydarooos wrote:Then for the first posted code the steps are :
2- the constructor reassign the value 0 to the member variable index
Yes, provided the no-arg constructor is not commented. Otherwise with the no-arg constructor commented, the index has a value 1, which it was assigned in the init block.
|
 |
Imad Aydarooos
Ranch Hand
Joined: Nov 02, 2010
Posts: 87
|
|
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.
I think its legal to declare initialization block before the declaration of the member variable, but the problem with the code is: its not trying to initialize the member variable, its accessing the member variable before its declaration
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3053
|
|
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.
|
 |
amit mandal
Ranch Hand
Joined: Aug 13, 2010
Posts: 46
|
|
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.
when the Twisty constructor is commented out:
1.super constructor of the Object class executes by the call inside the default constructor.
2.the init block of the Twisty class executes and initializes index=1.
3.since index is already initialized, the default constructor doesnt do it anymore.
4.so dd[index++][index++]==d[1][2]==4
when the Twisty constructor is present:
1.same as step 1.
2.same as step 2.
3.it explicitly assign 0 to index.
4.so dd[index++][index++]==d[0][1]==8
so first super constructor-->init block-->any constructor.
|
ocpjp 6.0 certified.
|
 |
swaraj gupta
Ranch Hand
Joined: Oct 22, 2010
Posts: 181
|
|
Hello Abimaran,
what do you mean by initializers in 4th point..?
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
|
 |
amit mandal
Ranch Hand
Joined: Aug 13, 2010
Posts: 46
|
|
swaraj gupta wrote:Hello Abimaran,
what do you mean by initializers in 4th point..?
i think by initializers Abhimaran meant the explicit initializers like int i=10;
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2928
|
|
swaraj gupta wrote:
what do you mean by initializers in 4th point..?
Initializers I suppose are- statements which initialize along with declaration.
Am not sure if Abimaran meant the same by Initializers.
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
Yes I do mean this,
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
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?
|
 |
amit mandal
Ranch Hand
Joined: Aug 13, 2010
Posts: 46
|
|
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?
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
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
No harm, just kidding. Thanks!
|
 |
Imad Aydarooos
Ranch Hand
Joined: Nov 02, 2010
Posts: 87
|
|
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.
Ok then looking to the beow cod segment:
1- THIS WILL COMPILE AND PRINT 10:
public class PreInit{
{
x = 10;
//System.out.println(x);
}
int x;
public static void main(String[] args){
PreInit pi = new PreInit();
System.out.println(pi.x);
}
}
2: This will produce (PreInit.java:4: illegal forward reference
System.out.println(x);
^
public class PreInit{
{
x = 10;
System.out.println(x);
}
int x;
public static void main(String[] args){
PreInit pi = new PreInit();
System.out.println(pi.x);
}
}
this shows that forward initialization is permitted, but forward accessing is forbidden
meaning: using the variable at the lift of (=) is permitted, and vise versa.
|
 |
 |
|
|
subject: Instance blocks.
|
|
|