Robert

39%
Flag icon
Programs that use int enums are brittle. Because int enums are constant variables [JLS, 4.12.4], their int values are compiled into the clients that use them [JLS, 13.1]. If the value associated with an int enum is changed, its clients must be recompiled. If not, the clients will still run, but their behavior will be incorrect.
Robert
Tangent: When is the value of an int "constant" _not_ compiled into clients? Consider the following: private static final int ONE = 1; private static final int TWO = 1 + 1; private static final int THREE = Integer.parseInt("3"); private static final int FOUR; static { FOUR = 4 } I'm guessing only 'ONE' is compiled into clients, but maybe also the 'TWO'? 'FOUR' could be used as a strategy to "hide" values from being compiled into clients so they become runtime-safe? Not advocating, just pointing out strangeness out of this optimization.
Effective Java
Rate this book
Clear rating
Open Preview