This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021.

Enumeration classes are a small but useful structure.

The basic concept

The enumeration type is enumerated type. In Java, it’s a special set of classes, declared with enums. You’re declaring a set of constants.

When Java runs, it is loaded in the method area.

It’s called in main, and it prints itself.

For common constants, we either create a separate class called Constant and define it like this:

public final String SPRING = "SPRING";
public final String SUMMER = "SUMMER";
public final String AUTUMN = "AUTUMN";
public final String WINTER = "WINTER";
Copy the code

Called by Constant.XXX, for example, to define spring:

String Season = constant. SPRING This assigns “SPRING” to the Season String.

But if XXX in.xxx is a finite set. Like the four seasons above, enumerations can be used to reduce redundancy.

Without further ado, let’s look at the code

The above example would look like this in enum:

enum Season {
    SPRING, SUMMER, AUTUMN, WINTER
}
Copy the code

Similarly, define a week:

enum WEEK {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
Copy the code

And of course there are months, so if you’re interested, you can write your own.

How do you use enumerated types?

Just use the Switch. I agree with Teacher Gao on this point. Enumerations are meant to be simple. A new class is better than a whole enumeration of advanced methods.

An enumerated class, though special, is also a class. Since it is a class, it has to be declared and then called.

After just defining two enum classes, we can initialize an enumeration object in the main function of this file with “class name object name = class.some enumeration type”.

public class TestEnum {
    public static void main(String[] args) {

        Season a = Season.AUTUMN;
        System.out.println("It is " + a + ".");

        WEEK today = WEEK.TUESDAY;
        System.out.println("Today is " + today + "."); }}Copy the code

Things to note: Enumerations are in the order written in the enum. This is very similar to enumerate in Python. In Python, the for loop iterates through key-value pairs for an object that can be enumerated:

for k,v in enumerate("happy") :print(k,":",v)
Copy the code

The running results are as follows:As you can see, the default is for each thrown value, and the matching key is incremented from 0.

Enumeration + Switch

Let’s take a look at the enumeration of moments in action:


Season a = Season.AUTUMN;
System.out.println("It is " + a + ".");

switch (a) {
    case SPRING:
        System.out.println("Spring is here, and it's time for animal XX.");
        break;
    case SUMMER:
        System.out.println("Summer is here, and it's swimming season.");
        break;
    case AUTUMN:
        System.out.println("Autumn is here and school is back.");
        break;
    case WINTER:
        System.out.println("Winter is here, and it's time to sleep 12 hours a day.");
        break;
}
Copy the code

Running results: