• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Execution order of Static Blocks, Constructors, and Instance Blocks

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

I am studying Chapter 3 of SCJP book, and I'm not sure if

(a) I don't understand the concept

or

(b) The book has a typo


Here is quote from Chapter 3 of the book,". An instance initialization block runs once every time a new instance is created. Remember when we talked about the order in which constructor code executed? Instance init block code runs right after the call to super() in a constructor, in other words, after all super-constructors have run."

Then the book gives sample code



However, the book says the following is the output:

1st static init
2nd static init

1st instance init
2nd instance init

no-arg const
1st instance init
2nd instance init

1-arg const

Questions

(1) Shouldn't no-arg const come before 1st instance init and 2nd instance init?
(2) Shouldn't 1-arg const come before 1st instance init and 2nd instance init?
(3) Did the book make a typo?


Thank you
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no typo, the order in the book is correct. Remember that in every constructor, if the very first statement is not this(), then compiler implicitly inserts a call to super() as a very first statement. Upon instantiation, after the call to super() completes, instance initializer blocks run, and then the remaining statements in the constructor run.
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sandra


It is written that instance blocks run right after all super constructors have run.
That means after running all super constructors when the execution reaches line 4
(See your code),, thatis Init() constructor then instead of running the constructor
code it will run the initialization blocks first (after all super construtors have run remember)
in the order in which they appear and print "1st instance init" and "2nd instance init" and then it will run the
current class constructor and print "no-arg const" .Similar process will happen when you call arg constructor
of Init class.
Hope this makes it clear to you.
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After reading your explanations, I re-traced the code and now the output makes perfect sense.

Thanks guys!
 
Sheriff
Posts: 6959
1317
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sandra, please check your private messages.
 
Did Steve tell you that? Fuh - Steve. Just look at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic