--Sample code to get an Enum constant from a value in Java --Build the sample code with the javac command and run it with the java command --Operation check environment: macOS Catalina + Java 8 (AdoptOpenJDK 1.8.0_242-b08)
Tiger linguistically supports the types listed in the Java ™ programming language. The simplest form of enumeration is similar to the C, C ++, and C # formats.
package com.example.enums;
/**Define a simple enum*/
public enum MetaName {
FOO, BAR, BAZ;
}
package com.example.enums;
/**Define an enum with one value*/
public enum OneValueEnum {
ONE(1), TWO(2), THREE(3);
private final int number;
/**
*Generate an enum constant by specifying a value.
*/
OneValueEnum(int number) {
this.number = number;
}
/**
*Prepare a method that returns a value.
*/
public int getNumber() {
return number;
}
/**
*Returns an enum constant that matches the value.
*/
public static OneValueEnum getByNumber(int number) {
//Processing that identifies and returns an enum constant from a value
for (OneValueEnum value : OneValueEnum.values()) {
if (value.getNumber() == number) {
return value;
}
}
return null; //If not identifiable
}
}
package com.example;
import com.example.enums.MetaName;
import com.example.enums.OneValueEnum;
public class Main {
public static void main(String[] args) {
testMetaName();
testOneValueEnum();
}
private static void testMetaName() {
try {
//You can get an Enum constant from a string using the auto-generated valueOf method
System.out.println("***** FOO *****");
MetaName foo = MetaName.valueOf("FOO");
System.out.println("foo.getClass=" + foo.getClass());
System.out.println("foo.getDeclaringClass=" + foo.getDeclaringClass());
System.out.println("foo.toString=" + foo.toString());
//IllegalArgumentException occurs when trying to get a constant that does not exist
System.out.println("***** bar *****");
MetaName bar = MetaName.valueOf("bar");
} catch (Exception e) {
e.printStackTrace();
}
}
private static void testOneValueEnum() {
try {
//You can get an Enum constant from a string using the auto-generated valueOf method
System.out.println("***** ONE *****");
OneValueEnum one = OneValueEnum.valueOf("ONE");
System.out.println("one.getClass=" + one.getClass());
System.out.println("one.getDeclaringClass=" + one.getDeclaringClass());
System.out.println("one.toString=" + one.toString());
System.out.println("one.getNumber=" + one.getNumber());
//In order to get the Enum constant that matches the value, you need to implement it yourself.
System.out.println("***** 1 *****");
OneValueEnum ichi = OneValueEnum.getByNumber(1); //Call your own implementation method
System.out.println("ichi.getClass=" + ichi.getClass());
System.out.println("ichi.getDeclaringClass=" + ichi.getDeclaringClass());
System.out.println("ichi.toString=" + ichi.toString());
System.out.println("ichi.getNumber=" + ichi.getNumber());
//Since there was no Enum constant that matches the value, null is returned.
// (Since it is self-implemented, you can do any processing)
System.out.println("***** 9 *****");
OneValueEnum kyu = OneValueEnum.getByNumber(9);
System.out.println("kyu=" + kyu);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Place the source code.
$ tree
.
└── src
└── com
└── example
├── Main.java
└── enums
├── MetaName.java
└── OneValueEnum.java
Create a directory to output the class file.
$ mkdir dest
Compile with the javac command.
$ javac -sourcepath src src/com/example/Main.java -d dest
Confirm that the class file has been generated.
$ tree
.
├── dest
│ └── com
│ └── example
│ ├── Main.class
│ └── enums
│ ├── MetaName.class
│ └── OneValueEnum.class
└── src
└── com
└── example
├── Main.java
└── enums
├── MetaName.java
└── OneValueEnum.java
$ java -classpath dest com.example.Main
***** FOO *****
foo.getClass=class com.example.enums.MetaName
foo.getDeclaringClass=class com.example.enums.MetaName
foo.toString=FOO
***** bar *****
java.lang.IllegalArgumentException: No enum constant com.example.enums.MetaName.bar
at java.lang.Enum.valueOf(Enum.java:238)
at com.example.enums.MetaName.valueOf(MetaName.java:3)
at com.example.Main.testMetaName(Main.java:23)
at com.example.Main.main(Main.java:9)
***** ONE *****
one.getClass=class com.example.enums.OneValueEnum
one.getDeclaringClass=class com.example.enums.OneValueEnum
one.toString=ONE
one.getNumber=1
***** 1 *****
ichi.getClass=class com.example.enums.OneValueEnum
ichi.getDeclaringClass=class com.example.enums.OneValueEnum
ichi.toString=ONE
ichi.getNumber=1
***** 9 *****
kyu=null
Recommended Posts