Can a enum have a main method.

May sound like a question asked in some of the entry level interviews. Of course, an enum can have a main method. Here is an example.

public enum MyEnum {
	KEY1,KEY2;
	
	public static void main(String[] args) {
		for (MyEnum enumVal : MyEnum.values()) {
			System.out.println(enumVal);
		}
	}
}

Leave a Comment