Java.lang Package Classes

Java Enum - finalize() Method



The java.lang.Enum.finalize() method is used to show that the enum classes cannot have finalize methods.

Syntax

protected final void finalize()

Parameters

No parameter is required.

Return Value

void type.

Exception

NA.

Example:

In the example below, the java.lang.Enum.finalize() method is used to show that the enum classes cannot have finalize methods.

import java.lang.*;

public class MyClass {
  
  //creating an enum
  public enum WeekDay{
    MON("1st"), TUE("2nd"), WED("3rd"), THU("4th"), FRI("5th");

    String day;
    WeekDay(String x) {
      day = x;
    }

    String showDay() {
      return day;
    }
  }

  public static void main(String[] args) {
    
    System.out.println("Enums cannot have finalize methods:");
    MyClass e = new MyClass() {
      protected final void finalize() {}
    }; 

    System.out.println("WeekDay List:");
    for(WeekDay i : WeekDay.values()) {
      System.out.println(i + " is the " + i.showDay() + " day of the week.");
    }
  }
}

The output of the above code will be:

Enums cannot have finalize methods:
WeekDay List:
MON is the 1st day of the week.
TUE is the 2nd day of the week.
WED is the 3rd day of the week.
THU is the 4th day of the week.
FRI is the 5th day of the week.

❮ Java.lang - Enum