my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem with instance initializer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem with instance initializer" Watch "Problem with instance initializer" New topic
Author

Problem with instance initializer

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066



The result is:
(Super) x = 0, y = 0
(Sub) x = 1, y = 0
.

How the first line is executed? Since the method in Super class doesn't have any 'body' in the method printVariable
Thanks in Advanced!

|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

My question is: In the Super class Constructor, we called the method printVariable, but there is no body in that method to print something in the console!
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

That is not a question but I get what your trying to say . Because you are using an instance of type Sub the overriden method in Sub is called;


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Since the object is the type of Sub class, all the methods and fields are related to Sub class, even if it's called in the Super class constructor. Is that so?
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
i have read that until the super constructor has completed the subclasses instance variables cant get initialised.....my question is when do the variables get loaded? why are thier value 0 0 when the flow of the program has not even reached whre the instance variables are declared?


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Hi, That is not my problem. I got the idea, That is :
The order of execution is:

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


But my problem here is, How a Sub class method is called in the Super class Constructor? If I delete the method in the Super class, It give a Compilation Error!
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

After the super constructor has been run and before the first statement after super() or this(); The reason of the 0, 0 output is because the variables haven't been initialized because the super constructor has not completed yet.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Abimaran Kugathasan wrote:
But my problem here is, How a Sub class method is called in the Super class Constructor? If I delete the method in the Super class, It give a Compilation Error!
Of course. Try to create just a Super object. Then there is no method printVariable;
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
but when do the instance variables get loaded? they do get thier default values...but when?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Have you read the topic? Because Abimaran Kugathasan made a nice list containing the answer to your question.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

In Java , when an object is created, initialization is done in that order previously I mentioned.

Wouter Oet says....
Of course. Try to create just a Super object. Then there is no method printVariable;


Sir, I couldn't catch the point. Please explain......
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

If you have just this code. What goes wrong?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Since there is no method in that name, Compilation fails. Ok. Here we are creating an object of type Sub class, so the method in the Sub class is invoked, then it is OK?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Now you're confused. I responded to your: " If I delete the method in the Super class, It give a Compilation Error!". The method must be in Super and can be in Sub.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

It's fine. Take a Super class called SuperX and two Sub classes called SubX and SubY and those three class have their own method printVariable() (Overriding). Both of these Sub classes extend the Super class SuperX, then in the SubX class if we create an instance of that class, and in SubX constructor we called SuperX constructor. In SuperX constructor, we called the method printVariable, so the method in the SubX is invoked, since we are creating an instance of class SubX.
Correct?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

That is correct.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with instance initializer