Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does marking a field as transient make it possible to serialise an object

public class Foo implements java.io.Serializable {
   private int v1;
   private static double v2;
   private Loan v3 = new Loan();
}

Options:
A. An instance of Foo can be serialized because Foo implements Serializable.
B. An instance of Foo cannot be serialized because Foo contains a non-serializable instance variable v3.
C. If you mark v3 as transient, an instance of Foo is serializable.
D. b and c

Answer: D

Explanation: An object may not be serialized even though its class implements java.io.Serializable, because it may contain non-serializable instance variables.

Now my question is:

As far as I know, transient is used to turn off serialization. Then how is transient in this case, helping us to serialize foo?

like image 298
zengr Avatar asked Mar 03 '11 05:03

zengr


People also ask

How does marking a field as transient makes it possible to serialize an object?

transient doesn't turn off serialization generally, but only for the field with which it's associated. Thus, serializing Foo would not transmit a value for the v3 field.

What does it mean if a field is declared as transient?

A field which is declare with transient modifier it will not take part in serialized process. When an object is serialized(saved in any state), the values of its transient fields are ignored in the serial representation, while the field other than transient fields will take part in serialization process.

Why do we mark a data member transient?

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.

What does transient mean with respect to serializing objects?

transient is a Java keyword which marks a member variable not to be serialized when it is persisted to streams of bytes. When an object is transferred through the network, the object needs to be 'serialized'. Serialization converts the object state to serial bytes.


1 Answers

transient doesn't disable serialization altogether; it just marks members that won't be serialized. It's typically used for stuff that would be incorrect or irrelevant when the object is unserialized, or stuff that it'd be less-than-safe to store (passwords, decrypted data, that sort of thing), or non-serializable stuff that could be easily reconstructed.

In this case, i assume the Loan class isn't serializable. (If it were, then A would be correct.) Marking v3 as transient just tells Java not to worry about that field, but go ahead and serialize the others. This means an unserialized Foo might have a null v3. If you want to store the Loan as well, you'd need to either keep track of enough info to recreate it at will, or change class Loan so that it implements java.io.Serializable as well.

Alternatively, there are methods you could implement (writeObject, readObject) if you need control over serialization. But that can be a bit of a hassle.

like image 151
cHao Avatar answered Nov 12 '22 13:11

cHao