Serialization is nothing but storing the state of the object. So we can serialize an object instance to a file or any stream. But can we serialize static members ? If yes then how? Since static members are tied to class not instance, so this can confuse that can be really serialize those static values? My answer is yes why not. The static values are tied to Class level but every class is itself a instance of java.lang.Class hence the static values are also part of instances of Class type. And hence we can serialize them. Now the tricky part is how? Here is a example :
We have a Class called StaticClass which has only one value as static.
package com.serialize; public class StaticClass { public static int x=20; }
We kept the value as public just for example. Now lets see how can we serialize this value. As I mentioned to serialize static value we need to have a instance of type java.lang.class and the serialize it. So our code for serialize is
package com.serialize; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class SerializeStaticClass { public static void main(String[] args) { try{ Class cls = Class.forName("com.serialize.StaticClass"); File file = new File("MyClass.obj"); FileOutputStream fileInputStream = new FileOutputStream(file); ObjectOutputStream objectInputStream = new ObjectOutputStream(fileInputStream); System.out.println("Serializing Class"); objectInputStream.writeObject(cls); System.out.println("Serializing Class Complete"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch(ClassNotFoundException e) { e.printStackTrace(); } } }
Now its time to write code to de-serialize and get the value back. Since we have used instance of Class type for serialization we will get the same Class type back while de-serialization . We will use reflection to retrieve the value from the instance of Class type.
package com.serialize; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.ObjectInputStream; public class DeserializeStaticClass { public static void main(String[] args) { FileInputStream fileInputStream = null; ObjectInputStream objectInputStream =null; File file = new File("MyClass.obj"); try{ fileInputStream = new FileInputStream(file); objectInputStream = new ObjectInputStream(fileInputStream); System.out.println("Reading object"); Class cls = (Class)objectInputStream.readObject(); int y=0; System.out.println("Value of x after deserialization is : "+cls.getField("x").getInt(y)); } catch(IllegalAccessException e) { e.printStackTrace(); } catch(NoSuchFieldException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
Now lets Run the Serialize class followed by the de-serialize class.
OUTPUT of SerializeStaticClass
Serializing Class Serializing Class Complete
OUTPUT of DeserializeStaticClass
Reading object Value of x after deserialization is : 20
So we got value of back from de-serializing the object of Class type. The serialize and de-serialize in this example can two tweaked further to achieve the desired purpose.