Win a copy of Svelte and Sapper in Action this week in the JavaScript 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Sybex OCPJP 8 - Enums

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
My bad, I read your reply badly, you are right.
 
author & internet detective
Posts: 40200
816
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic