• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Bizarre print statements that don't produce output, please help!

 
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On page 202 - 203 in the book 'Certified Associate Java SE 8 Programmer I' by Jeanne Boyarsky and Scott Selikoff, there is a code sample that is supposed to give output based on the order of initialization:

Until now, I have never seen print statements like those. We're not in the main method (or any method acually), so how is the the print statement in the block on line 6 supposed to work? And what are those nameless static blocks on lines 10 - 16?

The book says the output is supposed to be:

0
10
Torchie
constructor

But the way my code is now, it doesn't print anything. What is going on?
 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The bottom class needs to be in its own file.  Is it?

Line 6 is an instance initializer.  It runs before the constructor.

Lines 10 - 16 are static initializers.  They run, in order, when the class is first created.
 
Marshal
Posts: 76860
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please be careful about threa‍d names; I have looked for bizarre print statements and didn't find any, and I looked for print statements producing no output, and I didn't find any of them either . I think this is a different version of the same problem you encountered here.
 
Campbell Ritchie
Marshal
Posts: 76860
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you will find much about initialisers in the Java™ Tutorials. You can try the Java® Language Specification (=JLS) (also this JLS section), but I would have thought that your book would provide a better description of what initialisers look like.
 
Saloon Keeper
Posts: 26281
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nameless initializer blocks allow complex initialization of classes and class instances independent of constructors.

Effectively, nameless static initializers are executed in the order that they appear when the class is first loaded. Nameless non-static initializers are processed similarly except that in effect they are all bundled up into an invisible pre-constructor method that gets invoked before the actual constructors (if any) start processing. Or, if there are no constructors, when an object is instantiated.
 
Campbell Ritchie
Marshal
Posts: 76860
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doesn't that mean, after the superclass' constructor has completed and before any code in the current object's constructor(s) is executed?
 
Tim Holloway
Saloon Keeper
Posts: 26281
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Doesn't that mean, after the superclass' constructor has completed and before any code in the current object's constructor(s) is executed?



That would seem logical. Otherwise the anonymous initialiser code wouldn't be able to reference superclass properties, and that would be rather untidy.
 
author & internet detective
Posts: 41405
854
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
Please see pages 18 and 19 in chapter 1 that explain about instance initializers.
 
There's a city wid manhunt for this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic