Why is the static variable serialized?

public class MySerializable implements Serializable{ private int x=10; private static int y = 15; public static void main(String...args){ AnotherClass a = new AnotherClass(); AnotherClass b; //Serialize try { FileOutputStream fout = new FileOutputStream("MyFile.ser"); ObjectOutputStream Oout = new ObjectOutputStream(fout); Oout.writeObject(a); System.out.println( a.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //De-serialize try { FileInputStream fis = new FileInputStream("MyFile.ser"); ObjectInputStream Oin = new ObjectInputStream (fis); b = (AnotherClass) Oin.readObject(); System.out.println( b.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }catch (ClassNotFoundException e){ e.printStackTrace(); } } } class AnotherClass implements Serializable{ transient int x = 8; static int y = 9; @Override public String toString() { return "x : " + x + ", y :" + y; } } 

Could you tell me how the static variable is serialized?

+7
source share
5 answers

Apparently, static variables can be serialized (but you should not do this), since serialization is the process of maintaining the state of an instance of a class, and static variables are common to all instances. They say nothing about the state of the instance, so that would not make sense.

Suppose you are allowed to serialize a static variable. Then, when you deserialize the instance, you will get an old copy of this variable, which may have been changed since then. Since a static variable is common to all instances of a class, a change in a variable from any instance should be reflected in that instance.

Thus, they should not be serialized, because a variable in these conditions can violate its contract as a static variable.

Serialization : -

  • You should not serialize static variables ..

Deserialization : -

  • Instance will receive the static fields that have been loaded by the class. Thus, any changes that could be made to this variable will also be responsible for this instance.
+11
source

MySerializable Class current output below

 x : 8, y :9 x : 0, y :9 

In this case, the static variable is printed after the toString () method is called, at this point the value will be read from the class level variable.

Try the following:

Add this line of code to the MySerializable Class after //Serialize block

 AnotherClass.y = 5; 

output:

 x : 8, y :9 x : 0, y :5 

this means that the static variable is not stored in the file, it will be dynamically read by the toString () method.

+2
source

- Serialization used to save the state of an object during serialization, so during de-serialization, the saved state can be used to resurrect an Identical object on the heap.

- Yes, a static variable can be serialized , but it does not make sense .....

0
source

Static variables cannot be and are not serialized.

Your question seems to be based on the fact that you see the same value from a static variable after serialization as before serialization, but this is not due to the fact that the value is serialized and restored.

This is because the static initializer for this static variable sets it to 9, and it never changes.

To make sure that the static variables are not serialized, you can either perform the NPKR proposed change by changing the static field between serialization and deserialization, or you can do the following:

Run this program, then comment out the bit that performs serialization. As a result, you will get the old serialized version on disk.

Then change the static initializer of the static field to y = 5 and run the program again: you will get "x: 0 y: 5 as the output, because the value 9" of the static field has not been restored.

0
source

 import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; class TestJava implements Serializable{ public static int k = 10; public int j=5; public static void main(String[] args) { TestJava tj1= new TestJava(); TestJava tj2; try{ //serialization FileOutputStream fos = new FileOutputStream("myclass.ser"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(tj1); oos.close(); fos.close(); System.out.println("object serielized 1..."+tj1.j); System.out.println("object serielized 2..."+tj1.k); System.out.println("object serielized 3..."+k); k=++k; // 'k' value incrementd after serialization } catch(FileNotFoundException fnfe){ fnfe.printStackTrace(); } catch(IOException ioex){ ioex.printStackTrace(); } try{ //deserialization FileInputStream fis = new FileInputStream("myclass.ser"); ObjectInputStream ois = new ObjectInputStream(fis); tj2 = (TestJava) ois.readObject(); ois.close(); fis.close(); System.out.println("object DEEEEserielized 1..."+tj2.j); System.out.println("object DEEEEserielized 2..."+tj2.k); System.out.println("object DEEEEserielized 3..."+k); // in deserialization 'k' value is shown as incremented. // That means Static varialbe 'K' is not serialized. // if 'K' value is serialized then, it has to show old value before incrementd the 'K' value. } catch(FileNotFoundException fnfe){ fnfe.printStackTrace(); } catch(IOException ioex){ ioex.printStackTrace(); } catch(ClassNotFoundException CNFE){ CNFE.printStackTrace(); } } } /* Output of the above program object serielized 1...5 object serielized 2...10 object serielized 3...10 object DEEEEserielized 1...5 object DEEEEserielized 2...11 object DEEEEserielized 3...11 */ 
0
source

All Articles