Java enum Constructor

Java enum Constructor

What is Java enums?

In Java, an enumeration (or “enum”) is a special kind of class that represents a fixed set of values. Each value in an enum is called an enumeration constant, and it’s defined as a static final instance of the enum type. Enums are used to define a fixed set of values that a variable of that type can take on.

  • Enums are particularly useful when a variable can only take on one out of a small set of possible values. For example, in a card game, the suit of a card can be one of four values: hearts, diamonds, clubs, and spades

To know more about Java enum in java read the complete article.

Java enum constructor:

In Java, an enumeration (or “enum”) is a special kind of class that represents a fixed set of values. Each value in an enum is called an enumeration constant, and it’s defined as a static final instance of the enum type.

  • Enum constructors are always private, it’s not possible to change the access level.
  • Enum constructors are always called before any other code in the enum type, regardless of where they appear in the code.
  • Enum constructors can only be called by the enum itself, and it’s not possible to create new instances of the enum outside of the enum type.

Let’s look at a Java enum where the Java enum constructor is used to perform an operation on the given code.

Example 1 : Java enum constructor Method

Run

enum Size {

   // enum constants calling the enum constructors 
   SMALL("PrepInsta."),
   MEDIUM("PrepInsta."),
   LARGE("PrepInsta."),
   EXTRALARGE("PrepInsta.");

   private final String pizzaSize;

   // private enum constructor
   private Size(String pizzaSize) {
      this.pizzaSize = pizzaSize;
   }

   public String getSize() {
      return pizzaSize;
   }
}

class Main {
   public static void main(String[] args) {
      Size size = Size.SMALL;
      System.out.println(size.getSize());
   }
}

Output

PrepInsta.

Example 2 : Java enum constructor Method

Run
public class Main {
    public static void main(String[] args) {
        Size small = Size.SMALL;
        System.out.println("Abbreviation for small: " + small.getAbbreviation());

        Size medium = Size.MEDIUM;
        System.out.println("Abbreviation for medium: " + medium.getAbbreviation());

        Size large = Size.LARGE;
        System.out.println("Abbreviation for large: " + large.getAbbreviation());

        Size extraLarge = Size.EXTRA_LARGE;
        System.out.println("Abbreviation for extra large: " + extraLarge.getAbbreviation());
    }
}

enum Size {
    SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");

    private String abbreviation;

    private Size(String abbreviation) {
        this.abbreviation = abbreviation;
    }

    public String getAbbreviation() {
        return abbreviation;
    }
}

Output

Abbreviation for small: S
Abbreviation for medium: M
Abbreviation for large: L
Abbreviation for extra large: XL

Example 3: Java enum constructor

Run

public class Main {
    public static void main(String[] args) {
        Size small = Size.SMALL;
        System.out.println("Abbreviation for small: " + small.getAbbreviation());

        Size medium = Size.MEDIUM;
        System.out.println("Abbreviation for medium: " + medium.getAbbreviation());

        Size large = Size.LARGE;
        System.out.println("Abbreviation for large: " + large.getAbbreviation());

        Size extraLarge = Size.EXTRA_LARGE;
        System.out.println("Abbreviation for extra large: " + extraLarge.getAbbreviation());

        Size custom = Size.valueOf("CUSTOM");
        System.out.println("Abbreviation for custom: " + custom.getAbbreviation());
    }
}

enum Size {
    SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL"), CUSTOM("CUST");

    private String abbreviation;

    private Size(String abbreviation) {
        this.abbreviation = abbreviation;
    }

    public String getAbbreviation() {
        return abbreviation;
    }
}

Output

Abbreviation for small: S
Abbreviation for medium: M
Abbreviation for large: L
Abbreviation for extra large: XL
Abbreviation for custom: CUST

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription