Use transient keyword in not Serializable class
Asked Answered
H

5

6

Would it make sense to use the transient keyword in a class that does not implement Serializable?

Because classes that do not implement Serializable could still be serialized by the ObjectOutputStream.

Hydropic answered 3/8, 2013 at 16:52 Comment(0)
E
1

Because classes that do not implement Serializable could still be serialized by the ObjectOutputStream.

That is incorrect. That would throw a NotSerializableException.


The reason writeObject() takes an Object instead of Serializable is that the signature comes from implementing the interface ObjectOutput which is defined independent of serialization. But, it then prevents ObjectOutputStream from changing its signature.

public interface ObjectOutput {
  // ...
  void writeObject(Object obj);
}
Exedra answered 3/8, 2013 at 16:53 Comment(2)
So it would not make sense?Hydropic
This is an interesting comment, but it does not answer the OP's question.Santoro
W
7

You may want to still mark it as transient for several reasons. Two that immediately come to mind are:

  • It can help convey the semantic purpose of the field. I.e., it helps anyone reading your code understand what the variable to meant to be for

  • Third party libraries may make use of the keyword. For example, Google's Gson library can serialize/deserialize any object to/from JSON, regardless of whether it implements Serializable. In this case, Gson will (by default) skip fields marked as transient.

Wray answered 12/2, 2015 at 4:25 Comment(0)
M
2

Because object serialization is more complex than a simple implementations of Serializable (think about proxy: proxied object can implements Serializable, but not original object and in your code you still using original class)
Another way to implements serialization is Externalizable interface to have total control of your object serialization or (from javadoc):
Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:

private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException;
private void writeObject(java.io.ObjectOutputStream stream) throws IOException
private void readObjectNoData() throws ObjectStreamException;

look at ObjectOutputStream javadoc for more information about serialization process/mechanism.

EDIT: to answer your question, transient is a keyword used only in serialization context so a not Serializable object with a transient field doesn't make sense

Merle answered 3/8, 2013 at 17:7 Comment(0)
E
1

Because classes that do not implement Serializable could still be serialized by the ObjectOutputStream.

That is incorrect. That would throw a NotSerializableException.


The reason writeObject() takes an Object instead of Serializable is that the signature comes from implementing the interface ObjectOutput which is defined independent of serialization. But, it then prevents ObjectOutputStream from changing its signature.

public interface ObjectOutput {
  // ...
  void writeObject(Object obj);
}
Exedra answered 3/8, 2013 at 16:53 Comment(2)
So it would not make sense?Hydropic
This is an interesting comment, but it does not answer the OP's question.Santoro
G
1

Usually if the super class implements Serializable it's subclasses are serializable as well.This is the only situation when you can make fields transient and the class does't implement Serializable directly,but apart of this if the class is not serializable it makes no sense to make them transient.

Georgiana answered 3/8, 2013 at 17:10 Comment(1)
In that case, the class also implements Serializable, through inheritance.Poleyn
S
0

No, It would not make sense.

class State {
    // The 'transient' modifier has no effect here because
    // the 'State' class does not implement 'Serializable'.
    private transient int[] stateData;
}
Speechmaking answered 17/8, 2018 at 5:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.