Skip to content
Related Articles
Get the best out of our app
Open App

Related Articles

NotSerializableException in Java with Examples

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Serialization in Java is a mechanism of writing the state of an object into a byte-stream. It is mainly used in Hibernate, RMI, JPA, EJB, and JMS technologies.

The reverse operation of serialization is called deserialization where byte-stream is converted into an object. The serialization and deserialization process is platform-independent, which means you can serialize an object in a platform and deserialize it on a different platform.

In Java, a NotSerializableException exception is thrown when an instance of a class must implement the Serializable interface. The exception is thrown by either the serialization runtime, or by the instance of the class. The argument for the NotSerializableException is the name of the class.

The NotSerializableException class extends the ObjectStreamException class, which is defined as the superclass of all exceptions specific to Object Stream classes. Also, the ObjectStreamException class extends the IOException which signals that an I/O exception has occurred.

Class NotSerializableException

Note: All Implemented Interfaces are Serializable interface


public class NotSerializableException 
extends ObjectStreamException

Let us discuss the constructors of this class before a

  1. NotSerializableException(): Constructs a NotSerializableException object.
  2. NotSerializableException(String classname): Constructs a NotSerializableException object with message string.

Example 1:


// Java Program to Illustrate NotSerializableException
// Where Exception Is Thrown
// Importing required classes
// Class 1
// Helper class
class Employee {
    // Member variables
    private String id;
    // Member methods
    // Method 1
    // To get ID of an employee
    public String getId() { return id; }
    // Method 1
    // To set ID of an employee
    public void setId(String id)
        // this keyword refers to current object itself = id;
// Class 2
// Main Class
public class GFG {
    // Main driver method
    public static void main(String[] args)
        throws IOException
        // Create FileOutputStream class object  to
        // create a file
        FileOutputStream out
            = new FileOutputStream("employee.dat");
        // Similarly creating ObjectOutputStream class
        // object
        ObjectOutputStream outputStream
            = new ObjectOutputStream(out);
        // Creating objects of class 1
        Employee obj = new Employee();
        // Assifning ID to an employee
        // Writing objects to stream
        // Good practice is always to
        // Close the stream using close() method


Output : 

Errors in Code
Exception in thread "main" access denied ("" "employee.dat" "write")
at java.base/
at java.base/
at java.base/java.lang.SecurityManager.checkPermission(
at java.base/java.lang.SecurityManager.checkWrite(
at java.base/<init>(
at java.base/<init>(
at NotSerializableExceptionExample.main(                                                    

How to deal with the NotSerializableException

  • The simplest solution is to find the class that throws the exception and makes it implement the Serializable interface. However, this may not be feasible if the class that throws the exception belongs to a third-party library.
  • In case the class refers to non-serializable objects and these objects should not be serialized, then, you can declare these objects as transient. Once a field of a class is declared as transient, then, it is ignored by the serializable runtime.


Example 2:


// Java Program to Illustrate NotSerializableException
// where No Exception is Thrown Using Serializable interface
// Importing input output class
// By implementing Serializable interface
// we are allowing Student object to
// be stored in TestFile.txt
// Class 1
// Helper class extending to Serializable interface
class Student implements Serializable {
    // Member variables of this class
    int id;
    String name;
    // Constructor of this class
    public Student(int id, String name)
    { = id; = name;
// Class 2
// Main class
class Persist {
    // Main driver method
    public static void main(String args[])
        // try block to check for exceptions
        try {
            // Creating the object
            Student s1 = new Student(007, "Test");
            // Creating stream and writing the object
            FileOutputStream fout
                = new FileOutputStream("TestFile.txt");
            ObjectOutputStream out
                = new ObjectOutputStream(fout);
            // Closing the stream to free up memory space
            // using close() method
            // Display command to shown proper execution of
            // a program
                "Object stored successfully");
        // Catch block to handle the exceptions
        catch (Exception e) {
            // Print and display the exception on the
            // console


Object stored successfully


My Personal Notes arrow_drop_up
Last Updated : 05 Jan, 2022
Like Article
Save Article
Similar Reads
Related Tutorials