Prev Next

Java / Enum

Could not find what you were looking for? send us the question and we would be happy to answer your question.

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.

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

3. Can an enum be declared final?

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

4. Can an enum be declared abstract?

No.

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

		}

	};

	int payPerHour;

	Job(int payPerHour) {
		this.payPerHour = payPerHour;
	}

	abstract void whoAmI();

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

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.

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.

9. Default value for enum's serialVersionUID.

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

10. Are Enum constants static and final?

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

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 create a Singleton pattern, which is inherently Thread Safe.

Passing Enum as an argument to the static factory method makes method type safe.

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.

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.

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

}
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 myStaticMethod() {
		System.out.println("Enum static method");
	}

	// abstract void myAbstractMethod() ;

	public static void main(String s[]) {
		EnumExample.myStaticMethod();
	}
}

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

No. Java enum cannot be extended.

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);
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 EnumSet, EnumMap and compareTo method.

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 ';'.

20. Can an Enum extend another Enum in Java?

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

«
»
Arrays

Comments & Discussions