• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Constructor initialization order

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

I am using Kathy & Sierra's book. On Chapter 3, they have this exercise:



The output is : r1 r4 pre b1 b2 r3 r2 hawk

1) I do not understand the order of execution of "pre" and "hawk"
2) Are "pre" and "hawk" static variable block ?

I have already tried executing this with different blocks inside the main method and I still can't get it. Any help is welcome.

Thanks


[HENRY: formatted code to be more readable]
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, Charley. Welcome to JavaRanch!

Champ, I'll give you a tip: before posting a question here, please CarefullyChooseOneForum. In this case, I think your question will fit better in our Beginning Java forum. So, let's slide this post over there!
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

charley merleau wrote:


1) I do not understand the order of execution of "pre" and "hawk"
2) Are "pre" and "hawk" static variable block ?



No. "Pre" and "Hawk" are printed from the main() method.

Henry
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if you figured it out... but I'll try to summarize. You are dealing with "static initializer blocks", "constructors" and execution of "main" method. Here's what happens...

1. r1 - static initializer blocks execute as soon as a class is called. They execute in the order they appear in the code. (they only exist in Raptor here)
2. r4 - the next static initializer in Raptor executes
3. pre - main() is now executed, and the first line prints "pre"
4. b1 - the 2nd line of main() creates an instance of Hawk - this causes the initializer blocks (non-static) of the superclass to execute
5. b2 - then the constructor of the superclass executes
6. r3 - same thing, initializer block executes in Raptor (subclass of Bird)
7. r2 - same thing, constructor of Raptor executes
8. hawk - now that the instance of Hawk is all set, we move onto line 3 of the main() method which is to print "hawk"

initializer blocks are similar to the constructor except they are not inherited by their subclasses.

At least that's how I understand it... anyone else can chime in.

James
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James E Baker wrote:At least that's how I understand it


You explained it well and my understanding is the same.
 
charley merleau
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

Thanks so much for your answers. Yeah James, it is quite clear now. Happy to be part of this group.

But I think I still need further reading on this particular topic: that is shifting initialization block around and be able to determine the result of the program. Can somebody assist with good material on this topic? I have googled and didn't get anything quite matching.

A big Thank you for your help.

Charley.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

charley merleau wrote:Can somebody assist with good material on this topic?


I would say the book you are using is excellent material. I'm quite sure you won't find any better than that.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

charley merleau wrote:Can somebody assist with good material on this topic?


I would say the book you are using is excellent material. I'm quite sure you won't find any better than that.



I agree!!
 
Ranch Hand
Posts: 44
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,

I am fairly new to Java, so some stupid questions might pop-up:-P . Please bear with it. Help me clarify some doubts.

1. I was under the impression that execution of any code starts from main(). Is that true?
- if it is, then how come the code actually jumps to the class and accesses the static initializer blocks when there is no code accessing the class itself.

2. According to me, the order of execution is like this:

main() --> SOP("pre") --> "pre" is printed on screen --> new Hawk() --> //Raptor is accessed ---> //Bird is accessed --> // r1 and r2 printed ---> r1 r4 r3 r2 printed ---> SOP("Hawk") ---> hawk printed.



Please help me correct the order of execution.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should look at the execution of blocks when your class inherits from other class.
Your point is true when there is no inheritance.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nelo Angelo wrote:Please help me correct the order of execution.


The explanation of James E Baker is spot-on. That's what happens. When a class is loaded, static initializer blocks will run, because like their name indicates they are part of the initialization of the class (so they run before the main is executed).

Here is a little program to experiment a bit with (static) initializer blocks:


Before running this code you have of course to predict the output first
 
Nelo Angelo
Ranch Hand
Posts: 44
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, I got some insight into the working of static initializer blocks.

And, I am using the Complete Reference (5th Edition)Herbert Schildt. I hope I'm using the right material.
 
charley merleau
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Roel for the extra example.

I am happy I guessed the output right.
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic