SoFunction
Updated on 2025-03-08

Simple use of Java Enum

Recently, in order to facilitate the analysis of the description information of the status code, I learned about the use of Enum and found that it is quite useful.

First, define an Enum class Status, which has two attributes statusValue status codes and statusDesc status description

public enum Status {
  STATUS_OK("01","success"),
  STATUS_FAILED("02","fail"),
  STATUS_NOTHING("03","Unknown State");
  
  private Status(String statusValue, String statusDesc){
     = statusValue;
     = statusDesc;
  }
  
  //Get status description through statusValue  public static String getStatusDesc(String statusValue){
    for(Status s : ()){
      if((statusValue)){
        return ;
      }
    }
    return null;
  }
  
  //Rewrite the toString method  @Override
  public String toString(){
    return "statusValue=" +  + ",statusDesc=" + ;
  }
  
  private String statusValue;//Status value  private String statusDesc;//Status Description  public String getStatusValue() {
    return statusValue;
  }
  public void setStatusValue(String statusValue) {
     = statusValue;
  }
  public String getStatusDesc() {
    return statusDesc;
  }
  public void setStatusDesc(String statusDesc) {
     = statusDesc;
  }
}

Tested as follows

public class App {
  public static void main( String[] args )
  {
    (("01"));//Output: Success    (Status.STATUS_FAILED.getStatusDesc());//Output: Failed    (Status.STATUS_NOTHING.toString());//Output: statusValue=03, statusDesc=Unknown status  }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.