Friday 9 October 2015

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

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...