Java Serializable Interface:

Serialization in java can be achieved by implementing Serializable interface.

Serialization allows us to serialize the object or write the state of object as it is into byte streams. The reverse of the serialization is deserialization where byte stream converts back to object with the same state what we serialized.

For serialization java uses writeObject() and for deserialization it is readObject().

Declaring a variable as transient inside a class will not allow to serialize or write the object into byte stream.

Example

Below Employee class example is serializing the object state to byte stream using writeObject() method and for deserialization it is using readObject().

After deserialization name is printing Test as value and age is printing default value as 0. After declaring a variable as transient inside a class will not allow to serialize or write the object into byte stream. It only serialized name field and ignore age variable as it is declared as transient.

OUTPUT:

Note :Accessing readObject() and wirteObject() methods without implementing Serializable interface will throw NotSerializableException exception.

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan