java - Enum requires no recompilation of its clients - How? -


from effective java

you can add or reorder constants in enum type without recompiling client because fields export constants provide layer of insulation between enum type , clients: constant values not compiled clients in int enum pattern.

i understood this link int enum patterns compile-time constants. want know how enum internally works?

enums not compile-time constants. value not copied compile every class uses them. that's different int values, can compile time constants.

so if have class like

public class constants {     public static final int foo = 1; } 

and have class

public class client {     public static void main(string[] args) {         system.out.println(constants.foo);     } } 

the class print 1. change declaration of foo to

public static final int foo = 27; 

and recompile constants without recompiling client. execute client. printed value still 1, because has been copied compiler client class when client class compiled.

there no way have effect using enums. if store value instance variable of enum, or if refer ordinal(), you'll right value, without recompiling client class.


Comments