Java / Enum
Enum is nothing but the lists of constants, so it is useful when required to define a list of constants.
A Java Enum, introduced in Java 5, is a special kind of Java type used to define collections of constants. It is a special class type that holds constants, methods.
public enum DayOfTheWeek { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY; }
No. Compiler generates sub classes for each constants contained in the enum.
No.
Yes.
interface IJob { void jobRole(); } public enum Job implements IJob { JOB1(50) { @Override void whoAmI() { System.out.println("I am a Software Tester."); } @Override public void jobRole() { System.out.println("My role is to Test the Software applications to assess quality."); } }; int payPerHour; Job(int payPerHour) { this.payPerHour = payPerHour; } abstract void whoAmI(); }
Yes, Enum can implement interface in Java. An enum is a type, similar to class and interface, it can implement interface.
No, Enum can not extend class in Java.
By default, Enum extends abstract base class java.lang.Enum so it cannot extend another class as Java does not support multiple inheritance for classes.
Yes. Java enums are automatically Serializable, there is no need to explicitly add the "implements Serializable" clause following the enum declaration.
The serialVersionUID value for all Enums is 0L by default.
Yes. Enum constants are public static and final and can be accessed directly using enum name.
Enum provides type-safe; Enum variable can be assigned only with predefined enum constants otherwise it will issue compilation error.
Enum can be used in switch-case Java control statement.
Enum has its own namespace.
Enum constants can hold values specified at the creation time.
Enum can help create a Singleton pattern, which is inherently Thread Safe.
Passing Enum as an argument to the static factory method makes method type safe.
Enums are derived type as Java class and interface. Enum can have constructor, methods and variables(fields) and each Enum constant will have its own fields and methods.
No. Enum constructor cannot be declared as public. 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.
Yes. enum can have abstract methods. Every enum constant provide a different implementation abstract methods.
public enum Job { JOB1(50) { @Override void whoAmI() { System.out.println("I am a Software Tester."); } }, JOB2(55) { @Override void whoAmI() { System.out.println("I am a Business Analyst."); } }, JOB3(60) { @Override void whoAmI() { System.out.println("I am a Developer."); } }; int payPerHour; Job(int payPerHour) { this.payPerHour = payPerHour; } abstract void whoAmI(); }
Yes. enum can have startup main method that JVM executes.
public enum EnumExample { Ex1(1, 2), Ex2(2, 3); static enum InnerEnum { INNER1, INNER2 } int val1, val2; EnumExample(int val1, int val2) { this.val1 = val1; this.val2 = val2; } int myMethod() { return val1 + val2; } static void myStaticMethod() { System.out.println("Enum static method"); } // abstract void myAbstractMethod() ; public static void main(String s[]) { EnumExample.myStaticMethod(); } }
No. Java enum cannot be extended.
The values and the valueOf method are implicitly added by the compiler. Find the exact method signature below.
public static E[] values(); public static E valueOf(String name);
ordinal method returns the ordinal of this enumeration constant, its position in its enum declaration, where the initial constant is assigned an ordinal of zero. Most developers will have minimal use for this method. It is designed for use by sophisticated enum-based data structures, such as EnumSet, EnumMap and compareTo method.
Yes. we can create. However when the enum body has other members such as method, constructor then the first line must have a semicolon ';'.
No. All enums implicitly extend java.lang.Enum.