This discussion is archived
3 Replies Latest reply: Sep 2, 2010 8:41 PM by EJP RSS

Transient variable is serialized

843790 Newbie
Currently Being Moderated
I was following an example I found and I can not understand why the variable b is serialized when it is declared transient. The output I get is
Before:
Not Transient: Test1
Transient: Test2
wrinting object...
reading object...
After:
Not Transient: Test1
Transient: Test2      <<------ Should not be printed, right?
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class SerializableExample implements Serializable {
  private String a;

  private transient String b; // It´s declared transient

  public SerializableExample(String aa, String bb) {
    a = "Not Transient: " + aa;
    b = "Transient: " + bb;
  }

  public String toString() {
    return a + "\n" + b;
  }

  private void writeObject(ObjectOutputStream stream) throws IOException {
      System.out.println("Writing object...");
      stream.defaultWriteObject();
      stream.writeObject(b);
  }

  private void readObject(ObjectInputStream stream) throws IOException,
      ClassNotFoundException {
      System.out.println("Reading object...");
      stream.defaultReadObject();
      b = (String) stream.readObject();
  }

  public static void main(String[] args) throws IOException,
      ClassNotFoundException {
      SerializableExample sc = new SerializableExample("Test1", "Test2");
      System.out.println("Before:\n" + sc);
      ByteArrayOutputStream buf = new ByteArrayOutputStream();
      ObjectOutputStream o = new ObjectOutputStream(buf);
      o.writeObject(sc);
      // Now get it back:
      ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(
        buf.toByteArray()));
      SerializableExample sc2 = (SerializableExample) in.readObject();
      System.out.println("After:\n" + sc2);
    }
} 
  • 1. Re: Transient variable is serialized
    800025 Explorer
    Currently Being Moderated
    The transient variable is serialized in write/readObject.

    I added a third variable and now see what happens:
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
     
    public class SerializableExample implements Serializable {
      private String a;
     
      private transient String b; // It´s declared transient
      
      private transient String c; // Declared transient and not mentioned in read/writeObject
     
      public SerializableExample(String aa, String bb, String cc) {
        a = "Not Transient: " + aa;
        b = "Transient: " + bb;
        c = "Transient (not mentioned): " + cc;
      }
     
      public String toString() {
        return a + "\n" + b + "\n" + c;
      }
     
      private void writeObject(ObjectOutputStream stream) throws IOException {
          System.out.println("Writing object...");
          stream.defaultWriteObject();
          stream.writeObject(b);
      }
     
      private void readObject(ObjectInputStream stream) throws IOException,
          ClassNotFoundException {
          System.out.println("Reading object...");
          stream.defaultReadObject();
          b = (String) stream.readObject();
      }
     
      public static void main(String[] args) throws IOException,
          ClassNotFoundException {
          SerializableExample sc = new SerializableExample("Test1", "Test2", "Test3");
          System.out.println("Before:\n" + sc);
          ByteArrayOutputStream buf = new ByteArrayOutputStream();
          ObjectOutputStream o = new ObjectOutputStream(buf);
          o.writeObject(sc);
          // Now get it back:
          ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(
            buf.toByteArray()));
          SerializableExample sc2 = (SerializableExample) in.readObject();
          System.out.println("After:\n" + sc2);
        }
    } 
    Piet
  • 2. Re: Transient variable is serialized
    843790 Newbie
    Currently Being Moderated
    The output is correct. Which part confused you?
  • 3. Re: Transient variable is serialized
    EJP Guru
    Currently Being Moderated
    I can not understand why the variable b is serialized when it is declared transient.
    Because it is serialized manually here:
    stream.writeObject(b);
    and deserialized manually here:
    b = (String) stream.readObject();