VOOZH about

URL: https://www.geeksforgeeks.org/java/notserializableexception-in-java-with-examples/

⇱ NotSerializableException in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

NotSerializableException in Java with Examples

Last Updated : 23 Jul, 2025

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.

👁 Image

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.

Illustration: 

java.io
Class NotSerializableException
 java.lang.Object
 java.lang.Throwable
 java.lang.Exception
 java.io.IOException
 java.io.ObjectStreamException
 java.io.NotSerializableException

Note: All Implemented Interfaces are Serializable interface

Syntax:

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:

 
 

Output : 

Errors in Code
Exception in thread "main" java.security.AccessControlException: access denied ("java.io.FilePermission" "employee.dat" "write")
at java.base/java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
at java.base/java.security.AccessController.checkPermission(AccessController.java:897)
at java.base/java.lang.SecurityManager.checkPermission(SecurityManager.java:322)
at java.base/java.lang.SecurityManager.checkWrite(SecurityManager.java:752)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:225)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:126)
at NotSerializableExceptionExample.main(NotSerializableExceptionExample.java:21) 

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:

 
 
Output:

Object stored successfully


 

Comment
Article Tags:
Article Tags: