Enum constructor in Java

Java 8Object Oriented ProgrammingProgramming

The java.lang.Enum class is the common base class of all Java language enumeration types.

Here is the Enum constructor

Sr.NoConstructor & Description
1protected Enum(String name, int ordinal)
This is the single constructor.

The following is an example wherein we can create an Enum constructors −

Example

 Live Demo

enum Devices {
   LAPTOP(200), MOBILE(700), TABLET(400), DESKTOP(150);
   private int count;
   Devices(int p) {
      count = p;
   }
   int count() {
   return count;
   }
}
public class Demo {
   public static void main(String[] args) {
      Devices d;
      System.out.println("Count of Mobile devices : " + Devices.MOBILE.count() + " units\n");
   }
}

Output

Count of Mobile devices : 700 units
raja
Published on 20-Dec-2018 05:49:18
Advertisements