aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Object instantiation cycle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on Object instantiation cycle" Watch "Question on Object instantiation cycle" New topic
Author

Question on Object instantiation cycle

Yogesh Mashalkar
Greenhorn

Joined: Sep 20, 2005
Posts: 25
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

Joined: Aug 03, 2008
Posts: 9293
    
  17

Yogesh please Quote Your Sources...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Yogesh Mashalkar
Greenhorn

Joined: Sep 20, 2005
Posts: 25
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

Joined: Aug 03, 2008
Posts: 9293
    
  17

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

Joined: Apr 20, 2009
Posts: 804
    
    1

Instance methods are available after super constructor completes


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

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

Joined: Sep 20, 2005
Posts: 25
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

Joined: Sep 20, 2005
Posts: 25
Appreciate if someone clarifies as to when exactly do the instance variables get initialized to their default values!

Thanks!
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

I'd like to know that too

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

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

Joined: Sep 20, 2005
Posts: 25
Thanks Ankit!!
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

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

Joined: Jun 04, 2009
Posts: 155
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

Joined: Apr 20, 2009
Posts: 804
    
    1

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

Joined: Apr 20, 2009
Posts: 804
    
    1

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

Joined: Jun 04, 2009
Posts: 155
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

Joined: Apr 20, 2009
Posts: 804
    
    1

So if it doesn't exist why:
{a= new A();}
is legal?
Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83
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

Joined: Apr 20, 2009
Posts: 804
    
    1

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

Joined: Feb 16, 2009
Posts: 83
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Object instantiation cycle