• 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

Explicit initialization: It comes before the constructor is invoked, right?

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the above example, x is given its value before the constructor, Z( ), is invoked, correct?

If so, why does the following program print '0' and not '15' when an object of type B is created?



As I understand it, presumably wrong, the sequence of events is as follows:
1) Memory for A & B is zeroed on the heap.
2) Explicit Initialization occurs (which should assign 15 to x)
3) B's default constructor is invoked, which implicitly calls super(): A's constructor
3a) A's constructor is invoked, and eventually calls B's print( )
3b) B's print( ) displays the value of x, but prints zero instead of 15, why???
4) Control returns to the body of B's constructor, instance initializers are executed in order (although none exist here)
5) B's constructor completes and returns a reference to the new B object

So, where am I going wrong? Why is the output 0, and not 15?

Thanks in advance!
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chad,

The order you specify is not completely correct.

When you create a new object of type B the constructor for B's superclass (i.e. A)
is called and this is of course the no-argument constructor.

Once the super constructor (i.e. A's constructor) has returned, any instance variable
initialisation (and object initialiser block) is executed in class B.

Therefore, when in A the print() is called the instance variable x is not yet
initialised to 15 and that's why you get 0.

I hope this makes sense
 
Chad Michaels
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gian,

Thank you. From reading various books, I was under the impression that "explicit" initialization occurs before the constructor, and "instance initializers" are executed from within the constructor after the call to super() returns.

In any case, now I am wondering if explicit initialization occurs at the same time as instance initializers.
 
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
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


Compare you result with this.
 
Chad Michaels
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Abimaran,

The source of my confusion is this book, which isn't very good as I have found many mistakes, "SCJP Sun Certified Programmer for Java Platform, SE6 Study Guide" by Richard Raposa. In this book on page 101, it states the instantiation process is:

1) The JVM determines the amount of memory... memory is zeroed... (etc etc).
2) Explicit initialization of instance variables is performed.
3) The appropriate constructor is invoked, depending on the arguments specified in the new statement.
4) Before the constructor executes, one of the immediate parent class constructors is executed.
5) Any instance initializers are executed. If a class has multiple initializers, they are executed ... (etc etc).
6) The body of the constructor executes.
7) The new operator returns a reference to the new object.

So, if you notice number 2, 3, and 5, you can see why I was confused. According to the above rules, instance variables are initialized before the constructor is invoked, which appears to be incorrect.
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chad,

According to the example which you have given

1) Call to super
2) Explicit initialization of the class members ( Else the default values should persist);
3) Init blocks ( I have checked the order & the explicit initialization occurs before the init block starts executing)
4) Constructor Body

This should be the order. (Step 1 & 2 repeating for all classes in hierarchy before Object)

Please correct me if I am wrong.

Nice one. Thanks for posting.
 
Chad Michaels
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Avishkar,

Yes, it appears what you have listed is 100% correct. My problem was apparently from the explanation given in the book I referenced. If you notice steps 2 and 3:

2) Explicit initialization of instance variables is performed.
3) The appropriate constructor is invoked, depending on the arguments specified in the new statement.

This cannot be correct because "explicit" initialization would occur before the constructor is even invoked, which is obviously before super() because it comes from within the constructor. So, the example in the book I have is wrong.

I have taken note of your description. Very easy to remember. Thanks!
1) Call to super
2) Explicit initialization of the class members ( Else the default values should persist);
3) Init blocks ( I have checked the order & the explicit initialization occurs before the init block starts executing)
4) Constructor Body
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chad,

First let me thank you for putting a very nice post which is helping so many of us to understand a key concept (more deeply).

The call to super() is actually a instruction to initialize the inherited parts of the object instance.

For these same reasons

B()
{
super().print();// This will never compile
}

super() is not same as new A() (which makes perfect sense since A can also be abstract).

The call super is not the traditional call to a constructor which we might assume.

So thanks to you we know that above will not compile with substantial know-how we gathered in this post.

Keep on posting, keep on enriching.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic