- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can we serialize static variables in Java?
In Java, serialization is a concept using which we can write the state of an object into a byte stream so that we can transfer it over the network (using technologies like JPA and RMI).
But, static variables belong to class therefore, you cannot serialize static variables in Java. Still if you try to do so, the program gets compiled successfully but it raises an exception at the time of execution.
In the following java program, the class Student has a static variable and we are trying to serialize and desterilize its object in the another class named ExampleSerialize.
Example
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import java.io.Serializable; class Student implements Serializable{ private static String name; public Student(String name){ this.name = name; } public static String getName() { return name; } public static void setAge(String name) { Student.name = name; } } public class ExampleSerialize{ public static void main(String args[]) throws Exception{ Student std1 = new Student("Krishna", 30); FileOutputStream fos = new FileOutputStream("e:\student.ser"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(std1); FileInputStream fis = new FileInputStream("e:\student.ser"); ObjectInputStream ois = new ObjectInputStream(fis); Student std2 = (Student) ois.readObject(); System.out.println(std2.getName()); } }
Run time error
This program gets executed successfully but, while running you will get a run time exception as shown below −
Exception in thread "main" java.lang.ClassCastException: java.io.FileOutputStream cannot be cast to java.io.ObjectOutput at sample.TestSerialize.main(TestSerialize.java:31)
- Related Articles
- Can we initialize static variables in a default constructor in Java?
- Can we create non static variables in an interface using java?
- Can we access the instance variables from a static method in Java?
- Static variables in Java
- Can static variables be called using className.variableName in Java?
- Final static variables in Java
- Can we cast reference variables in Java?
- Can we make static reference to non-static fields in java?
- Can we override the static method in Java?
- Can we define a static constructor in Java?
- How can we serialize an array of objects using flexjson in Java?
- How can we serialize a list of objects using flexjson in Java?
- Static and non static blank final variables in Java
- Class and Static Variables in Java
- Can we Overload or Override static methods in Java?

Advertisements