Common methods in Enum class [Java]

Common methods in Enum class

Full class name of enum class: Java lang.Enum

First of all, we need to know that enum keyword defines enumeration classes, and enumeration classes defined with enum keyword directly inherit from Java Lang. enum class

Here we will explain the three most commonly used methods in Enum class

  • Here we will give an example of the methods in enum class. Here we first give a related enumeration class defined with enum keyword

    package Enumeration class.Definition of enumeration class.enum Keyword definition enumeration class;
    
    //Define enumeration class through enum keyword
    public enum Season1 {
        /*
        1. When defining an enum class through enum keyword, we first need to provide the object of the current enum class. Multiple objects are separated by (comma) and the end object is used; (end of seal)
    
        Note: many unimportant things are omitted when creating objects here, such as new keyword, variable type and constructor name
        Add: if the object is created with a parameterless constructor, then () can also be omitted
    
         */
        SPRING("spring","in the warm spring , flowers are coming out with a rush"),
        SUMMER("summer","Summer heat"),
        AUTUMN("autumn","fresh autumn weather"),
        WINTER("winter","a world of ice and snow");
    
        /*
        The following steps are the same as when we customize the enumeration class
         */
    
        /*
        2. Declare the attribute (constant) of the Season1 class, and use the private final keyword to decorate it
         */
        private final String seasonName;
        private final String seasonDesc;
    
        /*
        3. Privatize the constructor of the class and assign values to the properties of the object
    
        Note: using constructor assignment can make each of our objects have a unique constant
         */
        private Season1(String seasonName , String seasonDesc){
            this.seasonName = seasonName;
            this.seasonDesc = seasonDesc;
        }
    
        /*
        4. Other demands:
        4.1 Gets the properties of the enumeration class object
         */
        public String getSeasonName(){
            return this.seasonName;
        }
        public String getSeasonDesc(){
            return this.seasonDesc;
        }
    
        /*
        4.2 Provide the toString() method according to the requirements
    
        Note: even if we do not override the toString() method here, we will not output the address value when outputting this enumeration class object, because the enumeration classes defined with enum keyword are directly inherited
        java.lang.Enum Class, and the Enum class rewrites the toString() method inherited from the Object class. The output of the toString() method rewritten in the Enum class is the constant name corresponding to the enumeration class Object
         */
        @Override
        public String toString() {
            return "Season1{" +
                    "seasonName='" + seasonName + '\'' +
                    ", seasonDesc='" + seasonDesc + '\'' +
                    '}';
        }
    }
    

The following is our analysis of these three methods

  1. toString()
    • Returns the object name of the enumeration class object that calls this method
      • Therefore, the enum class defined by enum keyword has no special requirements. In fact, you don't need to rewrite the toString() method
    • This method is a non static method

eg:

//This Season1 class is the enumeration class defined by enum keyword given above
Season1 summer = Season1.SUMMER;
/*
Here, we will call the toString() method by default when outputting the summer object, but we still call it as shown here

The output here is: SUMMER
*/
System.out.println(summer.toString());
  1. values()
    • Call this method directly through the enumeration class to return the object array of the enumeration class, that is, combine the objects of the enumeration class into an array and return
    • This method is a static method
    • This method can easily traverse all enumerated values

eg:

/*
The Season1 class here is an enumeration class defined by enum keyword, and the enumeration classes defined by enum keyword are directly inherited from enum class. Therefore, the values() method called by Season1 class here is actually declared in enum class
*/
Season1 [] values = Season1.values();
for(int i = 0; i < values.length ; i++){
    System.out.println(values[i]);
}

eg:

  • In learning multithreading, we once learned that there is an internal class State in the Thread class. The State class is an enumeration class defined through the enum keyword, and several states of Thread operation are defined in the State class
Thread.State [] values = Thread.State.values();
for(int i = 0; i < values.length ; i++){
    System.out.println(values[i])
}
  1. valueof(String objName)
    • This method is called using an enumeration class You can convert a string into a corresponding enumeration object with the same name
    • This method is a static method

Note: the string must be the object name in the enumeration class calling this method. If not, a runtime exception will be thrown: illegalargumentexception (illegal parameter exception)

eg:

/*
A winter object is created by calling the valueof() method through the Season1 class and assigned to the reference winter of the Season1 type
*/
Season1 winter = Season1.valueof("WINTER");
//The output here is naturally the object name of the enumeration class object corresponding to the WINTER reference: WINTER
System.out.println(winter);

Keywords: Java Back-end

Added by johnnyblaze9 on Fri, 28 Jan 2022 03:04:50 +0200