This question is not about how to use Serializable, I already know that. However, I was wondering how implementing Serializable brings up the following warning:
The serializable class MyClass does not declare a static final serialVersionUID field of type long
Suppose I wanted to write an interface like Serializable, how would I go about “warning” the programmer implementing my interface to declare a variable? I’ve tried looking at the source code for Serializable.java but I couldn’t find anything which would result in this behaviour. Is this even possible?
Thanks!
I think the behavior you’re looking for would be better implemented using annotations and the annotation processor. The fact that the compiler warns you about not declaring
serialVersionUIDis handled by a different mechanism at the compiler level (and therefore, out of the reach of the programmer); it’s more of a coding convention enforced by the serialization API than a consequence of implementing theSerializableinterface.In fact, here you can examine the source code of
Serializableand as you can see, there’s nothing special about it for forcing a compiler warning: