Java enum Constructor

What are Enums? 

Enums are types that represent a fixed set of related constants. What does it mean? 

Let us break it down to make it simpler – 

Fixed Set – Enums have a fixed number of values specified upfront when defining an enum type. 

Related Constants – The fixed set of values for a given enum type defined. Hence, these enum values are related to their common enum type. 

Enums are types – Enums are types in themselves; they are in fact special types of classes. 

What is Java Enum Constructor? 

Java Constructor can only be Private or at the Package level, it can’t be public or protected hence access modifier public and protected are not allowed to Enum constructor, which will result in a compile-time error. 

Code:

Java Code

enum Size { 
SMALL("The size is small."), 
MEDIUM("The size is medium."), 
LARGE("The size is large."),
EXTRALARGE("The size is extra large."); 
private final String plate; 
// private enum constructor 
private Size(String plate) { 
this.plate = plate; 
} 
public String getSize() { 
return plate; 
}
}
public class TUF{
    public static void main(String args[])
    {
        Size size=Size.LARGE;
        System.out.println(size.getSize());
    }
} 

Output: The size is large.

Special thanks to Amisha Kumari for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article