NotSerializableException in Java with Examples
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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
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.
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.
public class NotSerializableException extends ObjectStreamException
Let us discuss the constructors of this class before a
- NotSerializableException(): Constructs a NotSerializableException object.
- NotSerializableException(String classname): Constructs a NotSerializableException object with message string.
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.
Object stored successfully