[Java] Enumeration type

//When accessing Season type SPRING, Season.SPRING
public enum Season {
  SPRING,
  SUMMER,
  AUTUMN,
  WINTER,
}
public class EnumConstClient {

  public static void main(String[] args) {
    var ecs = new EnumConstSeason();
    ecs.processSeason(EnumConstSeason.SPRING);
    ecs.processSeason(4);
    ecs.processSeason(EnumConstMonth.JANUARY);
  }
}

```java
public class EnumSeason {
  public void processSeason(Season season) {
    System.out.println(season);
  }

  public static void main(String[] args) {
    var es = new EnumSeason();
    es.processSeason(Season.SPRING);
 //Season型で定義されてない定数はエラー
 // es.processSeason (4); // Compile error
 // es.processSeason (EnumConstMonth.JANUARY); // Compile error
  }
}
``

# Enum members
 * ** name () **: Get the name of the enumeration constant
 * ** ordinal () **: Get ordinal number of enumeration constants
 * ** toString () **: Get the name of the enumeration constant, it may have been replaced with the appropriate string representation for each type
 * ** values () **: Get all enumeration constants
 * ** valueOf (String name) **: Get enumeration constant from name

```java
public class EnumMethod {
  public static void main(String[] args) {
    for (var se : Season.values()) {
      //Index number:Output by name q
      System.out.println(se.ordinal() + ":" + se.toString());
    }
    //Get enum from string
    var s = Season.valueOf("SPRING");
    System.out.println(s instanceof Season); //true
  }
}

Member definition

//Constructor call with enumeration constant
public enum Season {
    SPRING(0, "spring"),
    SUMMER(1, "summer"),
    AUTUMN(2, "autumn"),
    WINTER(4, "winter");

  //Field declaration
  private int code; //Season code
  private String jpName; //Display name
  //constructor
  private Season(int code, String jpName) {
    this.code = code;
    this.jpName = jpName;
  }
  //Method
  public int toSeasonValue() {
    return this.code;
  }
  //Getter ready
  @Override
  public String toString() {
    return this.jpName;
  }

  //public abstract void show();
}
public class EnumBasic {
  public static void main(String[] args) {
    System.out.println(Season.SPRING); //spring
    System.out.println(Season.SPRING.toString()); //spring
    System.out.println(Season.SPRING.toSeasonValue()); //0
  }
}

Have your own implementation of each enumeration constant

public enum Season {
  //Enumeration constant
  SPRING(0, "spring") {
    @Override
    public void show() {
      System.out.println("Spring is Akebono");
    }
  },
  SUMMER(1, "summer") {
    @Override
    public void show() {
      System.out.println("Summer is night");
    }
  },
  AUTUMN(2, "autumn") {
    @Override
    public void show() {
      System.out.println("Autumn is dusk");
    }
  },
  WINTER(4, "winter") {
    @Override
    public void show() {
      System.out.println("Work hard in winter");
    }
  };
  //Field declaration
  private int code; //Season code
  private String jpName; //Display name
  //constructor
  private Season(int code, String jpName) {
    this.code = code;
    this.jpName = jpName;
  }
  //Method
  public int toSeasonValue() {
    return this.code;
  }

  @Override
  public String toString() {
    return this.jpName;
  }
  //Functions that enumeration constants should implement (abstract methods)
  public abstract void show();
}
public class EnumMethod {
  public static void main(String[] args) {
    for (var se: Season.values()) {
      se.show(); //Spring is Akebono Summer is night Autumn is dusk Winter"
    }
  }
}

Bit field

//Case insensitive, multi-line mode enabled
var ptn = Pattern.compile("^[a-z0-9._-]*",
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);`
import java.util.regex.Pattern;

public class EnumBit {
  public static void main(String[] args) {
    var flags = Pattern.CASE_INSENSITIVE | Pattern.MULTILINE;
    if ((flags & Pattern.COMMENTS) != 0) {
      System.out.println("COMMENTS is valid.");
    }
  }
}
//Enumeration definition
public enum PatternFlag {
  UNIX_LINES,
  CASE_INSENSITIVE,
  COMMENTS,
  MULTILINE,
}
//Receive enums
Pattern.compile("^[a-z0-9._-]*",
EnumSet.of(PatternFlag.CASE_INSENSITIVE, PatternFlag.MULTILINE));

Recommended Posts

[Java] Enumeration type
Java type conversion
[Swift] Type type ~ Enumeration type ~
[JAVA] Stream type
Java Optional type
Java double type
[Java] Express Enum type without using Enum (enumeration) type
[Java] Data type ①-Basic type
[Swift] Shared enumeration type
[Java, Kotlin] Type Variance
Java class type field
Type determination in Java
Java study # 1 (typical type)
[Java] About enum type
[Java] Date type conversion
[Java] List type / Array type conversion
Java learning memo (data type)
[Java] Precautions for type conversion
[Java] Type conversion speed comparison
Java study # 7 (branch syntax type)
Java
Enumeration of all combinations Java
Java Primer Series (Type Conversion)
Java
[Java] Data type / matrix product (AOJ ⑧ Matrix product)
java (use class type for field)
[Java] Correct comparison of String type
How to use Java enum type
Java learning (0)
[Java Siler] About type inference by var
[Java] array
Java protected
[Java] Module
Java array
[Java] Implicit type cast (AOJ10-sum of numbers)
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java date data type conversion (Date, Calendar, String)
Java methods
Java method
Regarding String type equivalence comparison in Java
Java array
[Java] ArrayDeque
[Easy-to-understand explanation! ] Reference type type conversion in Java
java (method)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Use PostgreSQL data type (jsonb) from Java
Studying Java ―― 4
Java (set)
java shellsort
Find out how Java protects type integrity
[Java] compareTo
Studying Java -5
[Java ~ Variable definition, type conversion ~] Study memo