posted 14 years ago
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.)