Reputation: 5216
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 ??
Upvotes: 7
Views: 11177
Reputation: 49
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
*/
Upvotes: 0
Reputation: 1580
Static variables cannot be and are not serialized.
Your question seems to be based on the fact that you're seeing the same value from the static variable after serialization as before serialization, but this is not due to the value being serialized and restored.
This behavior is because the static initializer for that static variable sets it to 9, and it is never changed.
To verify that static variables are not serialized, you can either perform NPKR's suggested change, modifying the static field between serialization and deserialization, or you could do the following:
Run this program, then comment out the bit that performs the serialization. You'll have the old serialized version on disk as a result.
Then change the static initializer of the static field to y = 5
, and run the program again: you'll get 'x: 0 y: 5as the output, because the value
9` of the static field was not restored.
Upvotes: 0
Reputation: 5496
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.
Upvotes: 2
Reputation: 213193
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: -
Deserialization: -
Upvotes: 11
Reputation: 33534
- Serialization
is used to save the state of the object during serialization, so during the de-serialization the saved state can be used in order to resurrect an Identical object on the heap.
- Yes static
variable can be serialized
but it doesn't make any sense.....
Upvotes: 0