3. Enum Iteration in for-each loop:
- You can obtain an array of all the possible values of a Java enum type by calling its static values() method.
- All enum types get a static values() method automatically by the Java compiler. Here is an example of iterating all values of an enum:
- for (Day day : Day.values()) {
- System.out.println(day);
- }
- If you run this code, then you will get the following output:
- SUNDAY
- MONDAY
- TUESDAY
- WEDNESDAY
- THURSDAY
- FRIDAY
- SATURDAY
4. Enum fields:
- It is possible to add fields to Java Enum. Each constant enum value gets these fields. The field values must be supplied to the constructor of the enum when defining the constants.
- Here is an example:
- public enum Day {
- SUNDAY(1), MONDAY(2), TUESDAY(3), WEDNESDAY(4), THURSDAY(5),
- FRIDAY(6), SATURDAY(7);
- private final int dayCode;
- private Day(int dayCode) {
- this.dayCode = dayCode;
- }
- }
- The example above has a constructor which takes an int. The enum constructor sets the int field. When the constant enum values are defined, an int value is passed to the enum constructor.
- The enum constructor must be either private or package scope (default). You cannot use public or protected constructors for a Java enum.
5. Enum Methods:
- It is possible to add methods to Java Enum. Here I am showing an example that how to do it.
- public enum Day {
- SUNDAY(1), MONDAY(2), TUESDAY(3), WEDNESDAY(4), THURSDAY(5),
- FRIDAY(6), SATURDAY(7);
- private final int dayCode;
- private Day(int dayCode) {
- this.dayCode = dayCode;
- }
- public int getDayCode() {
- return this.dayCode;
- }
- }
- It is possible to call an Enum method through a reference to one of the constant values. The following code will show you how to call Java Enum method.
- Day day = Day.MONDAY;
- System.out.println(day.getDayCode()); //which gives 2.Because the dayCode field for the Enum constant MONDAY is 2.
- Java enums extend the java.lang.Enum class implicitly, so your enum types cannot extend another class. Please refer the following code. This code is from Java API.
- public abstract class Enum<E extends Enum<E>> implements Comparable<E>,Serializable {
- // Enum class code in Java API
- }
- If a Java enum contains fields and methods, the definition of fields and methods must always come after the list of constants in the enum. Additionally, the list of enum constants must be terminated by a semicolon;
- Java does not support multiple inheritance, and enum also does not support for multiple inheritance.
- Enums are type-safe. Enum has their own name-space. It means your enum will have a type for example “Day” in below example and you cannot assign any value other than specified in Enum Constants.
- Enum constants are implicitly static and final and cannot be changed once created.
- Enum can be safely compare using:
- Switch-Case Statement
- == Operator
- .equals() method
- You cannot create instance of enums by using new operator in Java because constructor of Enum in Java can only be private and Enums constants can only be created inside Enums itself.
- Instance of Enum in Java is created when any Enum constants are first called or referenced in code
- An enum can be declared outside or inside a class, but NOT in a method.
- An enum declared outside a class must NOT be marked static, final , abstract, protected , or private
- Enums can contain constructors, methods, variables, and constant class bodies.
- Enum constructors can have arguments, and can be overloaded.
- Enum constructors can have arguments, and can be overloaded.
- The constructor for an enum type must be package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body. You cannot invoke an enum constructor yourself
- package com.enum.exampleprogram;
- /**This is an Enum which indicates possible days in a week **/
- public enum Day {
- SUNDAY, MONDAY, TUESDAY, WEDNESDAY,THURSDAY, FRIDAY, SATURDAY;
- }
- package com.pamu.test;
- /** This class is an example of Enum **/
- public class EnumTest {
- Day day;
- public EnumTest(Day day) {
- this.day = day;
- }
- //method to get descriptions of the respected days in a switch-statement.
- public void getDayDescription() {
- switch (day) {
- case MONDAY:
- System.out.println("Monday is First day in a week.");
- break;
- case FRIDAY:
- System.out.println("Friday is a partial working day.");
- break;
- case SATURDAY:
- System.out.println("Saturday is a Weekend.");
- break;
- case SUNDAY:
- System.out.println("Sunday is a Weekend.");
- break;
- default:
- System.out.println("Midweek days are working days.");
- break;
- }
- }
- public static void main(String[] args) {
- EnumTest day1 = new EnumTest(Day.MONDAY);
- day1.getDayDescription();
- EnumTest day2 = new EnumTest(Day.TUESDAY);
- day2.getDayDescription();
- EnumTest day3 = new EnumTest(Day.WEDNESDAY);
- day3.getDayDescription();
- EnumTest day5 = new EnumTest(Day.FRIDAY);
- day5.getDayDescription();
- EnumTest day6 = new EnumTest(Day.SATURDAY);
- day6.getDayDescription();
- EnumTest day7 = new EnumTest(Day.SUNDAY);
- day7.getDayDescription();
- }
- }
Output:
- Monday is First day in a week.
- Midweek days are working days.
- Midweek days are working days.
- Friday is a partial working day.
- Saturday is a Weekend.
- Sunday is a Weekend.
Bagikan
Enum in java part 2
4/
5
Oleh
Kris Kimcil