• 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

[1Z0-815] Sybex 815 p317 - class initialization

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

From the book OCP STUDY GUIDE Java SE 11 Programmer 1, page 317 :


1. If there is a superclass Y of X, then initialize class Y first
2. Process all static variable declarations in the order they appear
3. Process all static initializers in the order they appear



I run this code :


and it outputs "1"

This means that the static initializer has been run before the variable declaration for j

What do you think?
 
Saloon Keeper
Posts: 4612
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adrian,

Welcome to the Ranch!

Try it out. Add in your static block the line ' System.out.println(j)', that comes in the code before j gets initialized, and see what happens.
 
Adrien Vergnaud
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello and thank you for your quick reply

Maybe my post was not very clear. The book states:


1. If there is a superclass Y of X, then initialize class Y first
2. Process all static variable declarations in the order they appear
3. Process all static initializers in the order they appear



But according to the StaticInit2 code example, the logic is:


1. If there is a superclass Y of X, then initialize class Y first
2. Process all static variable declarations and static initializers in the order they appear

 
Bartender
Posts: 1064
33
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adrien Vergnaud wrote:Hello and thank you for your quick reply

Maybe my post was not very clear. The book states:


1. If there is a superclass Y of X, then initialize class Y first
2. Process all static variable declarations in the order they appear
3. Process all static initializers in the order they appear



But according to the StaticInit2 code example, the logic is:


1. If there is a superclass Y of X, then initialize class Y first
2. Process all static variable declarations and static initializers in the order they appear



For what its worth, I believe what you are saying is correct.

It is generally considered pathological, normally unnecessary and confusing to the reader and maintenance programmer to have multiple static initializers for the same class, or to do anything tricky in terms of the initialization order.  It is almost always unnecessary and requires the reader/maintainer to remember rules they look at like, once every two years.

But the details are as you described, unless I have forgotten them from not looking at them for almost a year.
 
Adrien Vergnaud
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do agree with you

I'm studying the great book from Boyarsky - Selikoff. And this kind of code is "study material" for the exam
 
Marshal
Posts: 26750
81
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adrien Vergnaud wrote:And this kind of code is "study material" for the exam



Indeed. We're in a "certification" forum so it pretty much goes without saying that the code you see isn't production-ready professional-level code. But on the other hand it doesn't hurt to remind people of that from time to time.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code output is "1" but according to the  OCP STUDY GUIDE it should be "2". Just double checked using OpenJDK v.11.0.11+9-Ubuntu-0ubuntu2.20.04

Sorry but I still dont understand why code output doesnt correspond to OCP STUDY GUIDE
 
Jesse Silverman
Bartender
Posts: 1064
33
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The authors still visit this forum and will see this eventually, they more usually come by on the weekends.

For now I think you are correct, my comment that "You should not generally be writing code like this" doesn't detract from the fact that this is not new behavior, just a weird corner case that you are unlikely to see outside of an exam certification question.

I believe best style is to have at most one static { } and at most one non-static/instance { } initializer block.

As you know, the exams include many things you will hopefully never see in normal code, so good catch.
 
Master Rancher
Posts: 4002
52
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anatolii Shuba wrote:Sorry but I still dont understand why code output doesnt correspond to OCP STUDY GUIDE


Well, I would say it's because the book is wrong in this case.  Instead of:

2. Process all static variable declarations in the order they appear
3. Process all static initializers in the order they appear


it should say:

2. Process all static variable declarations and static initializers in the order they appear


Your code demonstrates this.  Also, the Java Language Specification backs us up:

JLS 16 12.4.2 wrote:9. Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block.


The book authors are very conscientious and responsible, and take a lot of care to present correct information.  But they are capable of making mistakes, and this seems to be one of them.  They will probably be along to correct this soon.
 
Anatolii Shuba
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:
it should say:

2. Process all static variable declarations and static initializers in the order they appear


Your code demonstrates this.  Also, the Java Language Specification backs us up:

JLS 16 12.4.2 wrote:9. Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block.




Now it is clear for me. Thanks!
 
author & internet detective
Posts: 40726
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made a note to look at this for the next update to the book. Thanks!
 
catch it before it slithers away! Oh wait, it's a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic