• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Instance blocks.

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Initialization blocks are executed after super class constructors. What have you done to prove this wrong?

Twisty() is not a super class constructor.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All classes extend the Object class.
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This prints 0. So the implicitly implemented constructor assigns a default value of 0.
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest you read what I've written in my first post and answer accordingly.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right Right, completely get it now. Thanks Stephan van Hulst
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right Right, completely get it now. Thanks Stephan van Hulst.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doesn't compile for me...
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which code doesn't compile? There're quite a few extracts above.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 46
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 46
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I do mean this,
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 46
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


No harm, just kidding. Thanks!
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic