What is a Type-safe Enum in Java?



In Java, an enum is a special class that represents a "group of constants" that cannot be changed, just like a "final variables". Java provides an enum keyword to create an enum, and inside the class, the constants are written in uppercase letters and separated by commas (,).

Syntax to create an enum in Java:

enum EnumName {
   CONST1,
   CONST2,
   CONST3
   // ...
}

Here,

  • enum: A predefined keyword used to define an enum.
  • EnumName: The name of the enum.
  • CONST1, CONST2, CONST3: The constant values that belong to the enum. These are fixed values and are written in uppercase.

Type Safe Enum in Java

In Java, typesafe enums are those enums that have their own namespace and ensure that only predefined constant values can be assigned. This means we cannot assign any arbitrary value to an enum variable, only constants defined within the enum can be used.

Note! Typesafe enums were introduced in Java version 1.5 to replace the old practice of using int or String constants, which were throwing more errors and were less readable.

Additionally, an enum is a reference type, which means that it behaves more like a class or an interface. As a programmer, we can create methods and variables inside the enum declaration.

Following is the basic syntax of the Typesafe Enum in Java:

enum EnumName {
   CONSTANT1 { void method() { // implementation } },
   CONSTANT2 { void method() { // implementation } };
   
   abstract void method();
}

Example 1

In the following example, we create a typesafe enum that restricts the values to only defined constants (i.e., permanent and contract), and the print() method ensures that only an array of JobTypes can be passed. Therefore, the compiler will check and prevent invalid values and ensure type safety:

import java.util.*;
enum JobType {
   permanent,
   contract
}
public class EnumTest1 {
   public static void main(String []args) {
      print(JobType.values());
   }
   public static void print(JobType[] list) {
      for (int i=0; i < list.length; i++) {
         System.out.println(list[i]);
      }
   }
}

The above program produces the following output:

permanent
contract

Example 2

Here is another example of the usage of typesafe enum. We define a type-safe enum JobType with constants "permanent" and "contract", each overriding the "abstract" print(String name) method. The enum allows only valid constants, the compiler ensures correct method usage, and type safety:

enum JobType {
   permanent {
      public void print(String str1) {
         System.out.println("This is a permanent " + str1);
      }
   },
   contract {
      public void print(String str2) {
         System.out.println("This is a contarct " + str2);
      }
   };
   abstract void print(String name);
}
public class EnumTest2 {
   public static void main(String[] args) {
      JobType dt1 = JobType.permanent;
      JobType dt2 = JobType.contract;
      dt1.print("job");
      dt2.print("job");
   }
}

Following is the output of the above program:

This is a permanent job
This is a contarct job

Enum vs Typesafe Enum in Java

Following are the few differences between enum and typesafe enum:

  • Enum is a group of named constants, whereas Typesafe Enum is a group of constants with specific behavior.
  • Enum ensures only enum values are used, whereas Typesafe Enum also enforces method implementation and restricts invalid values.
  • Enum does not support polymorphism, whereas Typesafe Enum supports it by allowing each constant to override methods.
Updated on: 2025-08-28T14:47:43+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements