Java14 released @serial annotations injava.io package. It is used to enable compile type checking of fields or functions for serialization-related implementation.

Serialization is the process of saving a state to Streams or files when an object is transferred from one VM to another machine.

Deserialization is to convert to an object from a stream.

To implement Serialization handling for a class,

Java provides two interfaces, that need to implement one of the interfaces

  • Serialization: Market interface, no methods
  • Externalization: Methods that need custom data handling and provide the following 5 methods

The following methods are annotated with @serial annotation to type-checking

private void writeObject(java.io.ObjectOutputStream stream) throws IOException
private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException
private void readObjectNoData() throws ObjectStreamException
MODIFIER Object writeReplace() throws ObjectStreamException
MODIFIER Object readResolve() throws ObjectStreamException

Field names

private static final ObjectStreamField[] serialPersistentFields
private static final long serialVersionUID

Serial annotation Example Usage

import java.io.Serializable
public class Employee implements Serializable {

    @Serial
    private static final long serialVersionUID = 12312312l;

    @Serial
    private Object readObject() throws IOException, ClassNotFoundException {

        return null;
    }

    @Serial
    private void writeObject() throws ObjectStreamException {

    }
    @Serial
    private void readObjectNoData() throws ObjectStreamException{

    }
    @Serial
    public Object writeReplace() throws ObjectStreamException{
      return null;
    }
    @Serial
    public Object readResolve() throws ObjectStreamException{
      return null;
    }

}

When you compile the code, it compiles the code into bytecode and does not check serialization data check

To do the type checking for serialization, You have to use below serial lint check option below as part of the Javac tool

javac -Xlint:serial Employee.

This flag complains about methods(five) signature not matched, wrong modifier, fields(2 fields) not matched

  • Checks if the class implements java.io.Serializable and declared serialization fields and methods
// not implemented Serializable interface
public class Employee  {

    @Serial
    private static final long serialVersionUID = 12312312l;

}
@Serial
private static final long serialVarsionUID = 123L; // compile error, Field must be 'serialVersionUID'
  • Serializable methods must be private It throws a compile error if readObjectNoData is public.
import java.io.Serializable;
public class Employee implements Serializable {
   // Compile error must be private
    @Serial
   public void readObjectNoData() throws ObjectStreamException{

   }
}

How to fix warning: [serial] serializable class has no definition of serialVersionUID

The warnings can be suppressed using @SuppressWarnings("serial") annotation

class Employee implements Serializable{
}

The compiler throws an error when you compile with javac -Xlint:serial Employee.java

warning: [serial] serializable class Employee has no definition of serialVersionUID class SerTest implements Serializable

@SuppressWarnings("serial")
class Employee implements Serializable{
}

You can check here more about this annotation

In Summary, @serial annotation helps developers to catch the errors at compile time errors before serialization files at runtime.

This has support on following IDES

  • Netbeans
  • IntelliJ IDEA
  • Eclipse