• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Which runs first within a constructor ?

 
Greenhorn
Posts: 12
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I'm preparing for the OCPJP8 exam and I have a question which I did not find an answer to in the guide book I'm using. My question is:

Which runs first within a constructor (right after the call to super ()), initialization of instance variables by their explicit values or instance initialization blocks?
 
author
Posts: 4274
34
jQuery Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first line of the constructor is always this() or super(), with super() being implicitly called if neither is specified.  That's executed first, meaning it pauses initialization of the current class to initialize the parent class.

Next, the instance variables and initializers are evaluated in the order in which they are declared in the file.  For example, you can't print a variable in an initializer block before it is declared.

Finally the rest of the body of the constructor is executed last.

Not sure what guide book you are using, but Jeanne and I covered this pretty well in both our OCA/OCP 8 and OCP 11 books.
 
Ouadi Belmokhtar
Greenhorn
Posts: 12
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scott Selikoff wrote:The first line of the constructor is always this() or super(), with super() being implicitly called if neither is specified.  That's executed first, meaning it pauses initialization of the current class to initialize the parent class.

Next, the instance variables and initializers are evaluated in the order in which they are declared in the file.  For example, you can't print a variable in an initializer block before it is declared.

Finally the rest of the body of the constructor is executed last.

Not sure what guide book you are using, but Jeanne and I covered this pretty well in both our OCA/OCP 8 and OCP 11 books.



Thank you very much Scott for your clear explanation.

Could you please specify in which chapter/section you covered this part in OCP8 book. I searched in table of content but did not find it.
 
author & internet detective
Posts: 40487
823
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pages 202-204 of our OCA 8 book cover it.
 
Ouadi Belmokhtar
Greenhorn
Posts: 12
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Pages 202-204 of our OCA 8 book cover it.



Thank you Jeanne.

 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic