An enumeration is its simplest terms is a list of named constants. Enumerations are a class which great expands on their capabilities, they have a number of methods that they can use which will be demostrated below.
Basic enumeration | enum Apple { Jonathan, GoldenDel, RedDel, Winesap, Cortland } public class EnumerationTest1 { public static void main(String[] args) { Apple a = null; a = Apple.Jonathan; System.out.println(a); if(a == Apple.Jonathan) { System.out.println("If Statement: It's a " + a); } switch (a) { case Jonathan: System.out.println("Case statement: It's a Jonathan"); default: System.out.println("Could not found apple type"); } } } |
All enumerations contain two predefined methods
values() and valueOf() examples | enum Apple { Jonathan, GoldenDel, RedDel, Winesap, Cortland } public class EnumerationTest2 { public static void main(String[] args) { Apple[] apples = Apple.values(); // returns an array for(Apple a : apples){ System.out.println(a); } System.out.println("\n" + Apple.valueOf("RedDel")); } } |
As enumerations are classes which means you have additional power, you can give them the follow
below is an example using some of the above
enumeration complex example | enum Apple { Jonathan(10), GoldenDel(9), RedDel(12), Winesap(15), Cortland(8); private int price; // price of each apple Apple(int p) { price = p; } // constructor int getPrice() { return price; } // getPrice method } public class EnumerationTest3 { public static void main(String[] args) { for(Apple a : Apple.values()) { System.out.println(a + " costs " + a.getPrice()); } } } |
Sometimes you may want to get the ordinal position of a value in a enumeration, you can use the ordinal method
Obtain enumeration ordinal position | enum Apple4 { Jonathan, GoldenDel, RedDel, Winesap, Cortland } public class EnumerationTest4 { public static void main(String[] args) { System.out.println(Apple4.valueOf("RedDel").ordinal()); // They start at zero } } |
You can embed supplemental information into a source file, this information is called an annotation, it does not change the program but provides meta-data to compiler, various IDE's, etc, they are heavy used in Java frameworks like Spring/SpringBoot. The @ symbol is used to tell the compiler that an annotation is being declared. Common annotations are @Interface, @Retention, @Override, @FunctionalInterface, @SuppressWarnings, @SafeVarargs and @Deprecated.
Annotation example | @Interface MyAnno { String str(); int val(); } |
You can create you own annotations
Create your own annotations | import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) // field annotation @interface JsonElement { public String key() default ""; } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) // method annotation @interface Init { } class Person { @JsonElement private String firstName; // default value is "" @JsonElement private String lastName; // default value is "" @JsonElement(key = "personAge") // set age to the key private String age; private String address; // fields don't have to have a annotation @Init private void initNames() { this.firstName = this.firstName.substring(0, 1).toUpperCase() + this.firstName.substring(1); this.lastName = this.lastName.substring(0, 1).toUpperCase() + this.lastName.substring(1); } // Standard getters and setters } public class AnnotationTest1 { ... } |