Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How come the static variable is 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;  
    }  
}

Can you please tell me how the static variable is serialized ??

like image 555
Girish Nair Avatar asked Oct 08 '12 05:10

Girish Nair


People also ask

Are static variables not serialized?

Static variables − The values of static variables will not be preserved during the de-serialization process. In-fact static variables are also not serialized but since these belongs to the class.

Are static methods serialized?

Static variables are not serialized.

How do you avoid variables to serialize?

You can prevent member variables from being serialized by marking them with the NonSerialized attribute as follows. If possible, make an object that could contain security-sensitive data nonserializable. If the object must be serialized, apply the NonSerialized attribute to specific fields that store sensitive data.

Why do objects need to be serialized?

Serialization allows the developer to save the state of an object and re-create it as needed, providing storage of objects as well as data exchange. Through serialization, a developer can perform actions such as: Sending the object to a remote application by using a web service.


2 Answers

Apparently, static variables can be serialized (But you should not do that), since serialization is the process of saving the state of an instance of a class, and static variables are common to all instances. They don't say anything about the instance's state, so, it wouldn't make sense at all.

Suppose you were allowed to serialize a static variable. Then, when you deserialize the instance, you will be getting an old copy of that variable, which might have been changed since then. Since the static variable is shared across all instances of the class, a change in the variable from any instance must be reflected in this instance.

So, they should not be serialized, because the variable under these conditions could possibly violate its contract as a static variable.

Serialization: -

  • Should not serialize the static variables..

Deserialization: -

  • Instance will get the static fields that was loaded with the class.. So, any changes that might have been done for that variable will be liable for this instance also..
like image 133
Rohit Jain Avatar answered Sep 30 '22 14:09

Rohit Jain


The Current output of MySerializable Class is below

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

In this case the static variable are getting printed after calling toString() method, by this time it will reads value from class level variable.

Try this:

Add this line of code in MySerializable Class after //Serialize block

AnotherClass.y = 5;

the output is :

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

this means the static variable is not storing in the file, it will read dynammically by toString() method.

like image 23
NPKR Avatar answered Sep 30 '22 13:09

NPKR