Saturday, August 14, 2010

Serialization: Network.java


Courtesy of education.oracle.com

Given: 

2. import java.io.*;
3. public class Network {
4.   public static void main(String[] args) {
5.     Traveler t = new Traveler();
6.     t.x1 = 7; t.x2 = 7; t.x3 = 7;
7.     // serialize t then deserialize t
8.     System.out.println(t.x1 + " " + t.x2 + " " + t.x3);
9.     }
10. }
11. class Traveler implements Serializable {
12.   static int x1 = 0;
13.   volatile int x2 = 0;
14.   transient int x3 = 0;
15. }

If, on line 7, t is successfully serialized and then deserialized, what is the result?
a) 0 0 0
b) 0 7 0
c) 0 7 7
d) 7 0 0
e) 7 7 0
f) 7 7 7

b is correct. Because static variables belong to the class and not to instances of a class, they are not serialized.

No comments:

Post a Comment