sterialize static variables in java
In Java, static variables are not serialized as part of the object’s state when you serialize an instance of a class. This is because static variables belong to the class itself, not to any specific instance. Serialization only captures the state of instance variables for a particular object.

Table of Contents
Why Static Variables Aren’t Serialized
- 1. Class-Level Scope: Static variables are shared among all instances of a class and belong to the class itself. They are not part of the instance’s state, which is what serialization captures.
- 2. Serialization Focus: Serialization is intended to capture and restore the state of individual objects. Since static variables are not part of any object’s state but are shared across all objects of a class, they are inherently excluded from the serialization process.
Step 1: Define a Serializable Class with Static and Non-Static Fields
```java
import java.io.Serializable;
public class Employee implements Serializable {
private static final long serialVersionUID = 1L; // Unique identifier for serialization
private String name;
private int id;
public static String companyName;
public Employee(String name, int id) {
this.name = name;
this.id = id;
}
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", id=" + id +
", companyName='" + companyName + '\'' +
'}';
}
}
```
Step 2: Serialize the Object
```java
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class SerializeDemo {
public static void main(String[] args) {
Employee.companyName = "Tech Solutions";
Employee emp = new Employee("John Doe", 12345);
try (FileOutputStream fileOut = new FileOutputStream("employee.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
out.writeObject(emp);
System.out.println("Serialized data is saved in employee.ser");
} catch (IOException i) {
i.printStackTrace();
}
}
}
```
Step 3: Deserialize the Object
```java
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class DeserializeDemo {
public static void main(String[] args) {
Employee.companyName = "Another Company";
Employee emp = null;
try (FileInputStream fileIn = new FileInputStream("employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn)) {
emp = (Employee) in.readObject();
} catch (IOException i) {
i.printStackTrace();
return;
} catch (ClassNotFoundException c) {
System.out.println("Employee class not found");
c.printStackTrace();
return;
}
System.out.println("Deserialized Employee...");
System.out.println("Name: " + emp.getName());
System.out.println("ID: " + emp.getId());
System.out.println("Company: " + Employee.companyName); // Note the static variable
}
}
```
Explanation
- Before Serialization:
- `Employee.companyName = “Tech Solutions”;`
- An `Employee` object `emp` is created with `name = “John Doe”` and `id = 12345`.
- After Deserialization:
- The static variable `companyName` is reset to `”Another Company”` before deserialization.
- When deserializing, you will notice that `companyName` reflects the new value `”Another Company”`, not the original `”Tech Solutions”`.
Key Points
- Static variables are class-level variables and are not part of the instance’s state.
- When an object is serialized, only the non-static instance variables are serialized.
- Static variables can be reset or changed independently of the serialized objects.
This behavior reinforces the understanding that static variables are not part of the serialized state and are not preserved during the serialization and deserialization process.