• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on Object instantiation cycle

 
Yogesh Mashalkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was solving a few questions when i stumbled on this one and it shaked my concepts on object instantiation cycle.



The output is :

1
5
2
5
6


Can anyone explain how on invoking the "this.Super()" in the superclass constructor, subclass instance variable "i" is initialized, even when the subclass constructor has not completed execution?

We need a concrete object to invoke a concrete implementation. How in the above code "this.Super()" invocation finds the subclass implementation, when the subclass object has not been constructed yet?

Please help!
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yogesh please Quote Your Sources...
 
Yogesh Mashalkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this question on site http://www.javablackbelt.com

In the Question manager list, question # 1028594

I have modified and executed the code on my machine to get the specified output.
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Who said that an object's methods are only available when the constructor completes. The restriction is that you can not access the members of the class before the super() call. And this can happen only in one case where you try to pass a field as parameter to the super class constructor. The super constructor is free to call any methods or use any fields of the base class...
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance methods are available after super constructor completes
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're just calling a method. The variable has to be initialized.

Personally, I think creating a method with the name Super, with a capital letter to make it look like a constructor, and essentially using a Java keyword for the method name, is just an attempt to confuse, as opposed to teach anything.

-Cameron McKenzie
 
Yogesh Mashalkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:The restriction is that you can not access the members of the class before the super() call.


So from what i understand here, we cannot access class members before super(), but member variables can be used from inside super().

This looked weird to me in the first place.

Everyone...thanks for the inputs!
 
Yogesh Mashalkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Appreciate if someone clarifies as to when exactly do the instance variables get initialized to their default values!

Thanks!
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to know that too

 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lukas Smith wrote:Instance methods are available after super constructor completes


No, everything including instance methods and fields are available in the super class's constructor too. The super class constructor doesn't needs to complete before they can be accessed.

You can stay on the same path and remember that instance variables gets initialized to their default values when the super class constructor is called. So if you try to access a sub-class field in super class's constructor, then they'll have their default values at that time...
 
Yogesh Mashalkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit!!
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:
Lukas Smith wrote:Instance methods are available after super constructor completes


No, everything including instance methods and fields are available in the super class's constructor too. The super class constructor doesn't needs to complete before they can be accessed.



So what do you think about it:
 
Eduardo Bueno
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lukas Smith wrote:

So what do you think about it:

Those are totally different code blocks. In the first one you are pointing an A reference in stack to a new object in heap. In the other one you are trying to print that reference.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those are totally different code blocks. In the first one you are pointing an A reference in stack - why?
a is an instance variable so it lives on the heap.
Code blocks are executed one by one.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:
Lukas Smith wrote:Instance methods are available after super constructor completes

So if you try to access a sub-class field in super class's constructor, then they'll have their default values at that time...

How can I do it?
 
Eduardo Bueno
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lukas Smith wrote:Those are totally different code blocks. In the first one you are pointing an A reference in stack - why?
a is an instance variable so it lives on the heap.
Code blocks are executed one by one.

Stop editing your messages, you're messing me up.

read cautiously, you're pointing TO not IN.
You answered the question yourself, code blocks are executed one by one, you're trying to print something that doesn't even exists.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if it doesn't exist why:
{a= new A();}
is legal?
 
Mo Jay
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, if you want to print something OR you want that :System.out.println(a); to do something you need to put it into main() method or use static block that will tell the compiler that this block belongs to the class and needs to be executed before anything else.

LOOK at your main() method there, you have nothing in it except you are instantiating the class: new Test1(); your default constructor to Test1 class has no initialization or execution. To resolve your issue you can do one of 2 things:

Either put it into main as follow:
public static void main(String[] args){
new Test1();
A a = new A();
System.out.println(a);
}

OR use a static block around your code as follow:
static{ A a= new A(); // its legal
System.out.println(a); //it is not legal
}


 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mo Jay wrote:
OR use a static block around your code as follow:
static{ A a= new A(); // its legal
System.out.println(a); //it is not legal
}


System.out.println(a); - it is LEGAL
 
Mo Jay
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I know it is Legal after you put the code in a static block, the bottom line is that you need something to get the ball running for your code and that's why there the main() method in the first place.

BUT to just throw a statement in there in the class without any referal or call to it from main(), constructor, method or static block then I think it is just not going to be executed properly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic