Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

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

 
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
  • 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?
 
Marshal
Posts: 6643
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 67464
257
  • Mark post as helpful
  • send pies
  • 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: 67464
257
  • Mark post as helpful
  • send pies
  • 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: 21603
147
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • 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: 67464
257
  • Mark post as helpful
  • send pies
  • 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: 21603
147
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • 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: 39789
797
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see pages 18 and 19 in chapter 1 that explain about instance initializers.
 
Yeah, but does being a ninja come with a dental plan? And what about this tiny ad?
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!