Master JDK1.5 enumeration types


As a new keyword introduced by sun, enum looks like a special class. It can also have its own variables, define its own methods, and implement one or more interfaces. When we declare an enum type, we should note that enum types have the following characteristics.

1. It cannot have a public constructor, which can ensure that the customer code cannot create a new instance of enum.

2. All enumeration values are public, static and final. Note that this is only for enumeration values. We can define any other type of non enumeration variables as we define variables in ordinary classes. These variables can use any modifier you want.

3. Enum implements the java.lang.comparable interface by default.

4. Enum overrides the toString method, so if we call color. Blue. Tostring(), the string “blue” will be returned by default

5. Enum provides a valueof method, which corresponds to the toString method. Calling valueof (“blue”) will return color. Blue. Therefore, we should pay attention to this when rewriting the toString method ourselves. We should rewrite the valueof method correspondingly.

6. Enum also provides the values method, which enables you to easily traverse all enumerated values.

7. Enum also has an ordinal method, which returns the order of enumeration values in enumeration classes. This order depends on the order of enumeration value declaration. Here, color. Red. Ordinal() returns 0.

With these basic features in mind, let’s see how to use them.

1. Traverse all enumerated values. Knowing the values method, we can easily use the foreach loop to traverse the enumerated values.

  for (Color c: Color.values())
  System.out.println(“find value:” + c);

2. Define methods and variables in enum. For example, we can add a method for color to return a color randomly.

  public enum Color {

* define a variable to represent the number of enumerated values.
* (I’m a little surprised why Sun didn’t directly provide a size method for ENUM)
  private static int number = Color.values().length ;

* an enumeration value is returned randomly
  @return a random enum value.
  public static Color getRandomColor(){
  long random = System.currentTimeMillis() % number;
  switch ((int) random){
   case 0:
    return Color.Red;
   case 1:
    return Color.Green;
   case 2:
    return Color.Blue;
   default : return Color.Red;

It can be seen that there is no difference between defining variables and methods in enumeration types and defining methods and variables in ordinary classes. The only thing to note is that variable and method definitions must be placed after all enumeration value definitions, otherwise the compiler will give an error.

3. Override toString, valueof method

We already know that enum provides toString, valueof and other methods. In many cases, we need to override the default toString method. What should we do with enum. In fact, this is no different from overriding the toString method of an ordinary class.

  public String toString(){
  switch (this){
  case Red:
   return “Color.Red”;
  case Green:
   return “Color.Green”;
  case Blue:
   return “Color.Blue”;
   return “Unknow Color”;

At this time, we can see that what is printed out with the previous traversal code is




You can see that toString is indeed overloaded. Generally speaking, when overriding toString, we should also override the valueof method to maintain their consistency.

4. Use constructor

Although enum cannot have a public constructor, we can still define a private constructor for use inside enum. Let’s use color as an example.

  public enum Color {
  Red(“This is Red”),
  Green(“This is Green”),
  Blue(“This is Blue”);

  private String desc;

  Color(String desc){
  this.desc = desc;

  public String getDesc(){
  return this.desc;


Here we provide a description for each color, and then define a constructor to accept the description.

Note that the constructor here cannot be public or protected, so as to ensure that the constructor can only be used internally, and the customer code cannot new an instance of an enumerated value. This is also entirely reasonable, because we know that the enumeration value is just a constant of public static final.

5. Implement specific interfaces

We already know that enum can define variables and methods. It needs to implement an interface, which is the same as that of an ordinary class. There is no example here.

6. Define the method of enumerating values.

As we saw earlier, we can define some methods for enum. In fact, we can even define methods for each enumeration value. In this way, the example of toString we overlaid earlier can be rewritten as follows.

  public enum Color {
  Red {
  public String toString(){
   return “Color.Red”;
  Green {
  public String toString(){
   return “Color.Green”;
  public String toString(){
   return “Color.Blue”;

Logically, this is clearer than the toString method that originally provided a “global”.

In general, enum, as a newly defined type, hopes to help programmers write more simple and understandable code. Personally, I don’t think it’s necessary to use too many advanced features of enum, otherwise it’s contrary to the original intention of being simple and understandable.

Recommended Today

Seven solutions for distributed transactions

1、 What is distributed transaction Distributed transaction means that transaction participants, transaction supporting servers, resource servers and transaction managers are located on different nodes of different distributed systems. A large operation is completed by more than n small operations. These small operations are distributed on different services. For these operations, either all of them are […]