Serializable and transient
Asked Answered
W

5

10

To make a class serializable we do the following:

class A implements Serializable {
    transient Object a;
}

And not this:

serializable class A {
   transient Object a;
}

Why, if we want to make a class serializable, do we implement a special interface. And if we want to exclude some fields we use the keyword transient? Why aren't special keywords used in both cases? I mean were there any reasons to make the same thing in different ways? I know, there is no such keyword as serializable but why wasn't it introduced instead of the special interface Serializable?

Waxplant answered 17/12, 2009 at 16:21 Comment(6)
Huh? I'm 100% confused... and your terminology isn't helping. For starters: if you want to make a class serializable, then you have the class implement the interface Serializable.Garik
That would meen to serialize only a value no?Polysyllabic
I also don't understand the question, but I totally agree that implementing the empty Serializable interface does seem like magic (which is not the same as magic numbers, btw).Pomander
But I think the question is asking why Serializable feels grafted on to the language. And I think the answer is because it is.Peer
@mmyers: then why is transient not? If anything, it must have been introduced later, because you can have the Serialization mechanism without transient members, but not the other way round.Shela
@Michael Borgwardt: Good question. It looks from java.sun.com/docs/books/jls/first_edition/html/1.1Update.html as if they put the transient keyword into the first edition of the language but didn't activate it until they added Serializable in 1.1.Peer
S
28

Why isn't used some special keyword to mark classes as serializable too? Serializable interface looks like a magic numbers in code and not like the language feature.

I think you have to look at it the other way: language keywords exist mainly to support compile-time language constructs. Serialization is a runtime mechanism. Additionally, you don't want to have an extra keyword for everything, because you then can't use it as an identifier. A marker interface on the other hand is much less intrusive.

The question is thus: why do we need a language keyword to mark transient fields? And the answer is that there simply was no other way to mark specific fields at that time.

Nowadays, one would use annotations for this purpose in both cases (and for other things like the obscure strictfp keyword as well).

Shela answered 17/12, 2009 at 16:33 Comment(4)
Thanks. I agree that transient was introduced only due to annotations absense. But I still can not understand why was not introduced "serializable" keyword.Waxplant
As I said: adding keywords to a language is actually something you want to avoid. With classes, it could be avoided by using a marker interface instead. With fields, there was no alternative.Shela
I've clarified the question a bit.Waxplant
This is the answer to the question. Given some feature X that you want to add, you try to avoid adding new keywords at all costs if there are already language constructs to handle it. Just because you have to compromise on one thing ("transient" in this case) doesn't mean you automatically turn everything else into a keyword too.Docent
Z
4

Serializable is a marker interface. Interfaces are a standard way (in Java and in some other languages) of indicating features of a class; an "is a" relaionship. Making Serializable an interface means we can declare methods that accept or return Serializables just like we can methods that work with other interfaces. Anything else would have required syntax changes to the language (at the time; now we have annotations, but I think an interface would still be used).

Zeitler answered 17/12, 2009 at 16:29 Comment(2)
I hope you don't write methods that have Serializable as parameter or return types.Recommit
Why not? For a general storage or communication interface, it may make perfect sense - your need stuff to be serializable, but that's all.Shela
W
1

Serializable is a marker interface (like Cloneable) that is used to set a flag for standard Java runtime library code that an object can be serialised according to the designer of that class.

The transient keyword can be used to specify that an attribute does not need to be serialised, for instance because it is a derived attribute.

See also this reply to a similar question on SO and this one about designing marker interfaces.

Update

Why marker interfaces and no keywords for things like serializable, cloneable, etc? My guess would be the possibility to consistently extend the Java runtime lib with new marker interfaces combined with too many keywords if behavioural aspects made it into the language.

The fact that class attributes cannot implement Interfaces and transient can be seen as a generic property of an attribute makes sense of introducing transient as a language keyword.

Wahlstrom answered 17/12, 2009 at 16:35 Comment(0)
H
0

So you're asking why you can't mark a class as not serializable (like a transient member)? Why wouldn't you just not mark class members of the not-to-serialize type as transient? Or use a serialization delegate for that class type when you do the serialization? It seems a little weird that you would want to tell Java to not do something at this level instead of telling it to do something.

Hecht answered 17/12, 2009 at 16:32 Comment(1)
Well, in boo you tell the compiler not to do that. Everything is serializable by default and there's the transient keyword for non-serializable stuff. No need for markers.Pomander
S
0

Transient keywords are used to protect a variable or a field from being stored and we do this to protect some sensitive information we just don't want to distribute at every place and we use Serializable interface to make a class Serializable. Although we can use Externalizable interface also but we prefer to use Serializable because of some advantages.

Go though this to clearly understand Serialization and transient keyword. http://www.codingeek.com/java/io/object-streams-serialization-deserialization-java-example-serializable-interface/

Spandrel answered 10/12, 2014 at 8:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.