Java 17 Recipes


-1. Defining a Fixed Set of Related Constants


Download 3.2 Mb.
Pdf ko'rish
bet129/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   125   126   127   128   129   130   131   132   ...   245
Bog'liq
Java 17 Recipes

7-1. Defining a Fixed Set of Related Constants
 Problem
You need a type that can represent a fixed set of related constants.
 Solution
Use an enum type. The following example defines an enum type, called FieldType, to 
represent various form fields you might find on the GUI of an application.
public enum FieldType { PASSWORD, EMAIL_ADDRESS, PHONE_NUMBER,
SOCIAL_SECURITY_NUMBER }


240
This is the simplest form of an enum type, which often suffice when needed is a 
related set of named constants. In the following code, a field variable of type FieldType 
is declared and initialized to the FieldType.EMAIL_ADDRESS enum constant. Next, the 
code prints the results from calling various methods that are defined for all enum types.
public static void main(String[] args) {
FieldType field = FieldType.EMAIL_ADDRESS;
System.out.println("field.name(): " + field.name());
System.out.println("field.ordinal(): " + field.ordinal());
System.out.println("field.toString(): " + field.toString());
System.out.println("field.isEqual(EMAIL_ADDRESS): " +
field.equals(FieldType.EMAIL_ADDRESS));
System.out.println(" field.isEqual(\"EMAIL_ADDRESS\"'): " +
field.equals("EMAIL_ADDRESS"));
System.out.println(" field == EMAIL_ADDRESS: " + (field == 
FieldType.EMAIL_ADDRESS));
// Won't compile – illustrates type safety of enum
// System.out.println(" field == \"EMAIL_ADDRESS\": " + (field == 
"EMAIL_ADDRESS"));
System.out.println("field.compareTo(EMAIL_ADDRESS): " +
field.compareTo(FieldType.EMAIL_ADDRESS));
System.out.println(" field.compareTo(PASSWORD): " +
field.compareTo(FieldType.PASSWORD));
System.out.println(" field.valueOf(\"EMAIL_ADDRESS\"): " +
field.valueOf("EMAIL_ADDRESS"));
try {
System.out.print("field.valueOf(\"email_address\"): ");
System.out.println(FieldType.valueOf("email_address"));
} catch (IllegalArgumentException e) {
System.out.println(e.toString());
}
System.out.println(" FieldType.values(): " + Arrays.toString 
(FieldType.values()));
}
Chapter 7 Data SourCeS anD ColleCtionS


241
Running this code result in the following output.
field.name(): EMAIL_ADDRESS
field.ordinal(): 1
field.toString(): EMAIL_ADDRESS
field.isEqual(EMAIL_ADDRESS): true
field.isEqual("EMAIL_ADDRESS"'): false
field == EMAIL_ADDRESS: true
field.compareTo(EMAIL_ADDRESS): 0
field.compareTo(PASSWORD): 1
field.valueOf("EMAIL_ADDRESS"): EMAIL_ADDRESS
field.valueOf("email_address"): java.lang.IllegalArgumentException:
No enum constant org.java17recipes.chapter07.recipe07_01.BasicEnumExample 
.FieldType.email_address
FieldType.values(): [PASSWORD, EMAIL_ADDRESS, PHONE_NUMBER, SSN]

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   125   126   127   128   129   130   131   132   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling