Advanced enum Concepts

Advanced Enum Features in Java

In the following video, we learn how to override the default integer values assigned to our Enum constants.



Defining Custom Values for enum Constants

Enums can contain constructors (but only private constructors), methods, variables, and constant class bodies. You can specify values of enum constants at the creation time as shown below.  The programmer cannot set a custom integer for an enum value directly, but one can define overloaded constructors that can then assign arbitrary values to self-defined members of the enum class. Defining getters allows then access to those self-defined members. The internal integer can be obtained from an enum value using the public getValue() method, and the list of enum values of an enumeration type can be obtained in order using the .values() method. MyEnum.values() returns an array of type MyEnum’s values.

Output:

When Are enums Created by the Java Virtual Machine?

Instances of enum in Java are created when any enum constants are first called or referenced in code. Because they are static, they are only created once and used throughout the run of the program.

More About enums

  • 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. enum constructors can have arguments (see above), and can be overloaded. 

  • enum constructors can NEVER be invoked directly in code. These constructors are called automatically when an enum is initialized.

  • an enum is a reference type, which means it behaves more like a class or an interface than a primitive. Thus, uninitialized enum variables start life with a null value.

  • an enum can have abstract methods.

  • An enum can implement one or more interfaces. When it does so, it must implement all the methods of the interfaces:

interface AnyInterface
{
    void methodOne();
     
    void methodTwo();
}
 
enum SomeEnum implements AnyInterface
{
    ABC, DEF, GHI;
     
    public void methodOne()
    {
        System.out.println("Method One");
    }
  public void methodTwo()
  {
      System.out.println("Method Two");
  }
}



Questions about Advanced enum Concepts

1 point
What will be the output of the following program?

enum Letters
{
    A, B, C;
     
    private Letters()
    {
        System.out.println(1);
    }
}
 
public class MainClass
{
    public static void main(String[] args)
    {
        Letters letter = Letters.B;
    }
}
1 point
What will be the output of the following program?

enum MyEnums
{
    FIRST, SECOND, THIRD, FOURTH;
}
  
public class Test
{
   public static void main(String[] args)
   {
       MyEnums[] myEnums = new MyEnums[4];
  
       for (int i = 0; i < myEnums.length; i++)
       {
           System.out.println(myEnums[i]);
       }
   }
}
1 point
What will be the output of the following program?

enum Levels
{
    TOP, MEDIUM(10), BOTTOM(20, 30);
     
    int i, j;
     
    private Levels()
    {
         
    }
     
    private Levels(int i)
    {
        this.i = i;
    }
     
    private Levels(int i, int j)
    {
        this.i = i;
         
        this.j = j;
    }
}
 
public class MainClass
{
    public static void main(String[] args)
    {
        System.out.print(Levels.TOP.i);
         
        System.out.print(Levels.TOP.j);
         
        System.out.print(Levels.MEDIUM.i);
         
        System.out.print(Levels.MEDIUM.j);
         
        System.out.print(Levels.BOTTOM.i);
         
        System.out.print(Levels.BOTTOM.j);
    }
}
1 point
What will be the output of the following program?

enum Enums
{
    ONE("ONE"), TWO("TWO"), THREE("THREE");
     
    String s = "NAME";
     
    private Enums(String s)
    {
        this.s = s;
    }
     
    void method()
    {
        System.out.println(s);
    }
}
 
public class MainClass
{
    public static void main(String[] args)
    {
        Enums.ONE.method();
         
        Enums.TWO.method();
         
        Enums.THREE.method();
    }
}
1 point
What will be the output of the following program?

enum MyEnums
{
    A, B;
     
    int i = 888;
}
 
public class MainClass
{
    public static void main(String[] args)
    {
        MyEnums.B.i = 999;
         
        System.out.println(MyEnums.A.i);
    }
}
1 point
Will the following program compile?

interface AnyInterface
{
    void methodOne();
     
    void methodTwo();
}
 
enum Enums implements AnyInterface
{
    ABC, DEF, GHI;
     
    public void methodOne()
    {
        System.out.println("Method One");
    }
}