Prev Next

Java / Enum

1. Why do we need enum?

Enum is nothing but the lists of constants, so it is useful when required to define a list of constants.

Read full answer

2. Define "enum".

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; }

Read full answer

3. Can an enum be declared final?

No. Compiler generates sub classes for each constants contained in the enum.

Read full answer

4. Can an enum be declared abstract?

5. Can an enum implement an interface?

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." ); } }; in...

Read full answer

6. Can an Enum implement a interface in Java?

Yes, Enum can implement interface in Java. An enum is a type, similar to class and interface, it can implement interface.

Read full answer

7. Can an Enum extend a class in Java?

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.

Read full answer

8. Is Enum Serializable in Java?

Yes. Java enums are automatically Serializable, there is no need to explicitly add the "implements Serializable" clause following the enum declaration.

Read full answer

9. Default value for enum's serialVersionUID.

The serialVersionUID value for all Enums is 0L by default.

Read full answer

10. Are Enum constants static and final?

Yes. Enum constants are public static and final and can be accessed directly using enum name.

Read full answer

11. Benefits of using Enums in Java.

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 c...

Read full answer

12. Are enums reference type in Java?

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.

Read full answer

13. Can a enum constructor be declared public in Java?

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.

Read full answer

14. Can enum have abstract methods in Java?

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 B...

Read full answer

15. Can an enum have main method in Java?

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 myStaticMet...

Read full answer

16. Can a Java enum be subclassed to add new elements?

No. Java enum cannot be extended.

Read full answer

17. What are the implicit methods in Java Enum?

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);

Read full answer

18. What is enum ordinal in java?

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 Enum...

Read full answer

19. Can we create an enum without constants?

Yes. we can create. However when the enum body has other members such as method, constructor then the first line must have a semicolon ';'.

Read full answer

20. Can an Enum extend another Enum in Java?

No. All enums implicitly extend java.lang.Enum.

Read full answer

«
»

Comments & Discussions