This is some information on the Java 6 certification that I prepare. This information is not very well structured and it is most of the time an answer to the questions that come when I read the SCJP 6 book (Bates and Sierra). *** AS OF 5 OF MARCH 2010, I AM SCJP 6.0 ***

Tuesday, January 12, 2010

11 - Example with transient (1)

What is the content of an int that was transient after the object is deserialized ?


public class Main implements Serializable {
  
    int a=5;
    transient int b=7;

    public static void main(String[] args) throws Exception {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Main.ser"));
        Main aMain = new Main();
        System.out.println("" + aMain.a + aMain.b);
        oos.writeObject(aMain);
        oos.close();
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Main.ser"));
        aMain = (Main)ois.readObject();
        ois.close();
        System.out.println("" + aMain.a + aMain.b);
    }
}


run:
57
50

As you can see the variable that is transient is assigned the integer zero.

No comments:

Post a Comment

Followers