Why can't I serialize an object without a no-arg constructor even though it implements Serializable?

StackOverflow https://stackoverflow.com/questions/22332034

Question

I am trying to serialize a UUID with the Titan graph database and I believe I cannot because per the definition of Serializable, java.util.UUID fails to provide a no-arg constructor.

(See top of http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html for this)

Given that java.util.UUID claims to implement Serializable, what's going on here? Is this just a historical mistake that can't be removed for backward compatbility, or is there a specific technical reason for this weird (and to me, annoying) choice?

Update: Per the answer, UUID actually is Serializable and I misread the no-arg constructor rule which is subtle and does not actually require a no-arg constructor in the same class. The reason I couldn't serialize mine is that Titan (or its dependencies) adds a stricter no-arg declared constructor rule -- not core Java. I was confused as to who was imposing this rule, Java or Titan.

Était-ce utile?

La solution

A Serializable class doesn't need a no-args constructor. Its nearest non-serializable base class does.

You've misread the specification.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top