CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Serializable Interface in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

Serializable Interface in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 3 min read ยท 0 Comments
Share: in X

The Serializable interface in Java is a marker interface that indicates that a class can be serialized. Serialization is the process of converting an object’s state into a byte stream. Which can then be transmitted over a network or stored in a persistent storage medium such as a file. The byte stream can later be used to recreate the object in memory.

Table of Contents

  • How to make Java class serializable?
    • Example
  • What is Serialization and Deserialization in Java?
  • Serialize object in Java
  • Deserialize object in Java
  • Exclude from serialization in Java
  • Homework

How to make Java class serializable?

To make a Java class serializable, the class must implement the Serializable interface. This interface does not have any methods that need to be implemented. It simply serves as a flag to indicate that the class can be serialized.

Example

Following is an example of a serializable class in Java:

import java.io.Serializable;

public class Person implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

What is Serialization and Deserialization in Java?

Serialization is the process of converting the state of an object into a byte stream. This byte stream can then be saved to a file, sent over a network, or stored in a database. And Deserialization is kind of the opposite of Serialization. This means, converting back to a Java object from a byte stream.

Serialize object in Java

To serialize object of this class, we can use the ObjectOutputStream class.

Following is an example of how to serialize an object and write it to a file:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class SerializeObjectDemo {
	public static void main(String[] args) {
		Person person = new Person("Virat Kohli", 30);

		try (FileOutputStream fos = new FileOutputStream("Person.txt");
				ObjectOutputStream oos = new ObjectOutputStream(fos)) {
			oos.writeObject(person);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Deserialize object in Java

To deserialize object in Java, we can use the ObjectInputStream class.

We can see an example of how to read the serialized object from a file and recreate it in memory in the following code:

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class DeserializeObjectDemo {
	public static void main(String[] args) {
		Person person;

		try (FileInputStream fis = new FileInputStream("Person.txt");
				ObjectInputStream ois = new ObjectInputStream(fis)) {
			person = (Person) ois.readObject();
			System.out.println("Name: "+person.getName());
		} catch (IOException | ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}

Output:

Name: Virat Kohli

Exclude from serialization in Java

It’s important to note that not all objects are serializable. For example, objects that contain references to non-serializable objects cannot be serialized. In these cases, we can use the transient keyword to exclude certain fields from the serialization process.

import java.io.Serializable;

public class Employee implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;
    private int age;
    private transient String password;  // exclude from serialization

    public Employee(String name, int age, String password) {
        this.name = name;
        this.age = age;
        this.password = password;
    }

    // getters and setters
}

In this Employee class, when we try to serialize the field password will be excluded and when we deserialize and print the value of that field it will be null.

Homework

You can try to use the Employee class to serialize and deserialize and let me know in the comment section below what will be the output.

Related Posts:

  • Marker Interface in Java
  • Control Statements in Java
  • (Solved) Java 8 date/time types are not supported by default
  • MySQL Commands for Developers
  • java.io Package Overview
  • Interface in Java: Mastering Abstraction and…
Tags:javalanguage-fundamentals
Was this article helpful?
โ† Previous ArticleCommand Line Arguments in Java
Next Article โ†’Boolean Data Type in Java

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap