• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about constructing enums

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I stumbled onto the following. Given:

enum C { D, E, F, G, H;
C(){out.println("A");}
}

as a top-level enum.

I notice if I don't instantiate a C, then the ctor is never called. But if I instantiate one or more C's, e.g. Cc = C.E; then it doesn't matter how many C's I instantiate; the enum ctor always runs 5 times. (Or, more generally, the enum ctor runs x times, where x is the number of enum constants.)

Does anyone know if there's additional discussion about this somewhere, or what the principles are? I was quite surprised to see this. (I would have thought that the ctor would run for each enum constant that the program references. But when the program references one constant, ALL of them are apparently instantiated.)
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That exactly works as the way it should! Read below:

Each one of the enum constants is in fact a “Final Static Variable” of the enum type, and each one of those is initialized to object at the time the enum first loads (means first time you see enum name in your code!) so this means essentially each one of the enum constants is initialized and refer to an object of the enum type on the heap.
Meaning that in the enum declaration below:

enum Colors {
BLUE, WHITE, RED;
private Colors() {

System.out.println("Constructor!");
}
}
BLUE is in fact:
final static Colors BLUE = new Colors();

So when the enum is first loads the constructor is run for each of those constants and that’s why you see the constructor is ran “constant times” even though you declare only one of the enum constants!
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Leonard Fischer please use full names, don't use short names in your query
I think you want to say constuctor instead of ctor.
If you use full names we can understand easily what you want to say.
 
Leonard Fischer
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the very interesting answer, and sorry for not using full names (like constructor); I'll try to be careful about that in the future.
 
A teeny tiny vulgar attempt to get you to buy our stuff
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic