• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Sybex OCPJP 8 - Enums

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Jeanne & Scotts book OCP Java SE8 Prog..II page 23. "The first time that we ask for any of the enum values, Java constructs all of the enum values. After that, Java just returns the already-constructed enum values." The following code doesn't seem to bear this out. What am I missing?

 
Greenhorn
Posts: 13
4
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried with the following main method:

The output is


The 2 values seems instantiated when the enum class is initialized. Is it your issue ?
 
Roy Pozarelli
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The issue, as I see it, is that there are 2 enum constants ONCE & TWICE, but they are not both instantiated at the time of the first one.
 
Roy Pozarelli
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bad, I read your reply badly, you are right.
 
author & internet detective
Posts: 42074
932
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
You can also see this by only referencing one of the enums.



The output of this is still the two lines. Which shows that you get both enums created even if you only ever use one.

 
Paper jam tastes about as you would expect. Try some on this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic