Java Magazine, March/April 2016
ORACLE COM JAVAMAGAZINE MARCH APRIL 2016 43 new to java This extension effectively adding a parameter list to the enum value invokes the enums constructor The expression within the parentheses is the actual parameter passed to the constructor The enum object is still of class CommandWord as before but we are now storing a string attribute inside it And the value for this attribute is passed in to our enum object via its constructor We can store any number and type of attributes inside an enum object that is in instance fields just like in any other object Read our CommandWord definition again it should all slowly come together and start to make sense now The great advantage of this scheme is that we can now use a String again to recognize the typed word for example help by comparing the input string against the command strings stored inside our enums but our program logic is independent of these strings Earlier I mentioned that another problem with our first version was internationalization If we decide to translate our program into a different language lets say the help command is now hilfe we run the danger of introducing errors If we just change the command words in the array the program will compile but not function none of the commands will be recognized but we dont get an error The problem is that the strings are not only used for input but also for the program logic That is bad In our new enum version that problem has been resolved The actual command strings are mentioned only once if they change they need to be changed only in one location and the program logic works with logical values the enum constants that will continue to work In practice the input commands would be read out of a locale dependent text file but the principle is the same Under the Hood Enums are really implemented as classes and enum values are their instances There is little special about this and knowing this helps us understand how they work and what we can do with them Enum classes all automatically inherit the Java standard class Enum from which they inherit some potentially useful methods it also means that they cannot extend another class The inherited methods you should know about are name ordinal and the static method values The name method returns the name exactly as defined in the enum value The ordinal method returns a numeric value that reflects the order in which the enums were declared starting with zero For example CommandWord cmd CommandWord GO System out println cmd name System out println cmd ordinal will print GO 0 In practice these two methods are much less useful than you might first think Your code typically should not depend on the actual enum name so the name method is not often useful it is much better to override and use the toString method for that purpose and if you write your code well you will rarely need the ordinal number The static values method is more often useful It returns an array of all enum values and can be used to iterate over them Heres an example CommandWord ca CommandWord values for CommandWord cw ca System out println cw
You must have JavaScript enabled to view digital editions.