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
.
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
.
Because classes that do not implement
Serializable
could still be serialized by theObjectOutputStream
.
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);
}
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.
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
Because classes that do not implement
Serializable
could still be serialized by theObjectOutputStream
.
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);
}
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.
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;
}
© 2022 - 2024 — McMap. All rights reserved.