Java5 enumeration class details and example code

  • 2020-05-26 08:27:37
  • OfStack

Enumeration (enum) type is a new feature added to Java 5, which is a new type that allows specific data fragments to be represented as constants, all in a type-safe form.

1. Use of constants

Before JDK 1.5, we defined constants as: public static fianl... . Now, with enums, you can group related constants into one enum type, and enums provide more methods than constants.


package com;
 
public enum Color {
   
   RED, GREEN, BLANK, YELLOW 
 
}

use


package com;
 
public class B {
 
  public static void main(String[] args) {
 
 
    System.out.println( isRed( Color.BLANK ) ) ; // Results:  false
    System.out.println( isRed( Color.RED ) ) ;  // Results:  true
 
  }
 
 
  static boolean isRed( Color color ){
    if ( Color.RED.equals( color )) {
      return true ;
    }
    return false ;
  }
 
}

Or the use of switch


package com;
 
public class B {
 
  public static void main(String[] args) {
 
    showColor( Color.RED );
 
  }
 
  static void showColor(Color color){
    switch ( color ) {
    case BLANK:
      System.out.println( color );
      break;
    case RED :
      System.out.println( color );
      break;
    default:
      System.out.println( color );
      break;
    }
     
  }
}

2. Custom functions


package com;
 
public enum Color {
   
   RED(" red ", 1), GREEN(" green ", 2), BLANK(" white ", 3), YELLO(" yellow ", 4);
   
   
  private String name ;
  private int index ;
   
  private Color( String name , int index ){
    this.name = name ;
    this.index = index ;
  }
   
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getIndex() {
    return index;
  }
  public void setIndex(int index) {
    this.index = index;
  }
   
 
}

use


package com;
 
public class B {
 
  public static void main(String[] args) {
 
    // The output of a 1 The value of the enumeration 
    System.out.println( Color.RED.getName() );
    System.out.println( Color.RED.getIndex() );
 
    // Traverse all enumerations 
    for( Color color : Color.values()){
      System.out.println( color + " name: " + color.getName() + " index: " + color.getIndex() );
    }
  }
 
}

The results of

red

1

RED name: red index: 1
GREEN name: green index: 2
BLANK name: white index: 3
YELLO name: yellow index: 4

Conclusion:

1. The essence of enumeration is a class. Before enumeration, you can still use the most basic programming means of java to solve the problem where you need to use enumeration. Enums mask the type information for enum values, unlike variables defined with public static final, which must specify the type. Enumeration is an extensible template for building constant data structures. The use of enums enhances the robustness of the program, such as the compiler reporting an error when referring to a nonexistent enum value. More USES of enumeration need to be researched and created in development. Java5 and Java6 have many new features. The technology is upgrading, so it is necessary for programmers to learn if you love java. If you can't read the code that someone else USES for a new feature, that's depressing.

2. Enumeration only accounts for a small proportion in the Java family, so I didn't use enumeration in many projects. After all, a project is developed and maintained by many people. So most constants are defined by public static final.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: