final static
with constants that have no meaning in the value itself and have meaning as a symbolpublic final static int SPRING = 0;
is an int type, so all numbers can be received.enum type name
//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
}
}
;
to separate the constant from the member//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
}
}
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"
}
}
}
//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