Tuesday, 18 June 2019

Serialization | Java Enums are Inherently Serializable

enums are automatically Serializable, there is no need to explicitly add the "implements Serializable".

import java.io.Serializable;

enum Day {
     SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}

public class EnumTest {
     public static void main(String[] args) {
          Day day = Day.MONDAY;
          String isSerializable = "Not Serializable";

          if (day instanceof Serializable) {
               isSerializable = "Serializable";
          }

          System.out.println(isSerializable);
     }
}
Output: Serializable

In the above code, we can see that the Day enum is instanceof the Serializable.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...