Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are readObject and writeObject private, and why would I write transient variables explicitly?

I am reading the chapter on Serialization in Effective Java.

  1. Who calls the readObject() and writeObject()? Why are these methods declared private ?

  2. The below is a piece of code from the book

    // StringList with a reasonable custom serialized form public final class StringList implements Serializable {     private transient int size = 0;     private transient Entry head = null;      //Other code      private void writeObject(ObjectOutputStream s)         throws IOException {         s.defaultWriteObject();         s.writeInt(size);         // Write out all elements in the proper order.         for (Entry e = head; e != null; e = e.next)            s.writeObject(e.data);         }     } } 

    Is there any specific reason the variable size is declared as transient and then in the writeObject method it is explicitly written? If it were not declared as transient, it would have been written anyway, right?

like image 782
Vinoth Kumar C M Avatar asked Sep 19 '11 06:09

Vinoth Kumar C M


People also ask

What is readObject and writeObject?

writeObject method writes the byte stream in physical location. To use the default mechanism to save the state of object, use defaultWriteObject. readObject method is used to read byte stream from physical location and type cast to required class. To read the data by default mechanism we use defaultReadObject .

What's the purpose of transient variables?

Transient in Java is used to mark the member variable not to be serialized when it is persisted to streams of bytes. This keyword plays an important role to meet security constraints in Java. It ignores the original value of a variable and saves the default value of that variable data type.

When the data is transient which we need to use?

transient is a variables modifier used in serialization. At the time of serialization, if we don't want to save value of a particular variable in a file, then we use transient keyword. When JVM comes across transient keyword, it ignores original value of the variable and save default value of that variable data type.

What will happen to transient variable after Deserialization?

With Transient variable we can stop serializing the required values, but after deserialization we are getting default values of transient variables and we are loosing the original values. So then what is the need of creating transient variable instead we can skip creating the variable itself.


1 Answers

(1) The methods are not declared in any class or interface. A class, that implements the Serializable interface and requires special special handling during the serialization and deserialization process must implement those methods and the serializer/deserializer will try to reflect those methods.

This is one of the rather strange corners in Java where the API is actually defined in the javaDoc... But if the methods had been defined in an interface, then they had to be public (we can't implement an interface method an lock it by adding a private modifier).

Why private - the javaDoc does not give a hint. Maybe they are specified as private because no other class but the implementor is intended to use them. They are private by definition.

(2) The example simply shows how the special handling works. In this example, size is transient and will not be serialized. But now we introduce the special handler and this handler adds the value of size to the stream. The difference to the normal approach with non-transient fields could be the order of elements in the resulting stream (if it matters...).

The example could make sense, if the transient field was defined in a super class and a subclass wanted to serialize the value.

like image 108
Andreas Dolk Avatar answered Sep 28 '22 04:09

Andreas Dolk