• 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

Problem with instance initializer

 
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


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!
 
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
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!
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
 
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
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?
 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
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
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but when do the instance variables get loaded? they do get thier default values...but when?
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you read the topic? Because Abimaran Kugathasan made a nice list containing the answer to your question.
 
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
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have just this code. What goes wrong?
 
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
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
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
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is correct.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic