3

I've been playing with the serializable interface for a little pet project of mine for a while, and I often notice a warning about how I should define static final long serialVersionID.

I've been searching for a way to make my own interfaces produce a similar warning (forcing/reccomending the declaration of a constant in whatever class implements it) and haven't found anything so far. The Seralizable interface is just a marker interface, so it doesn't actually contain anything and the closest I've ever came to answering my question is with this thread on dreamincode.

Can anybody help, because that sounds like a very useful feature?

Santo Guevarra
  • 385
  • 4
  • 8

2 Answers2

0

The easiest is to simply declare a value. Something like:

private static final long serialVersionUID = 1L;

The reason to do this is to control serialization/deserialization of your objects. If you serialize an instance of a class that does not have a serialVersionUID, the system generates one from the current class definition. If you change the class in any significant way, the system-generated id will change. This, in turn, will prevent you from deserializing an object serialized with the old id.

If you're serializing objects but not persisting them, then defining an ID is probably unnecessary (except to get rid of the compiler warning).

See this thread for more info.

Community
  • 1
  • 1
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • My question was about how to add a warning regarding that. Maybe I should have bean clearer... – Santo Guevarra Mar 17 '13 at 05:42
  • @SantoGuevarra - Ah. You probably cannot do that. You can force implementing classes to implement a _method_, but the warning about `serialVersionUID` is generated by the compiler (or lint tool) as a special case and is specific to the `Serializable` interface. – Ted Hopp Mar 17 '13 at 05:48
0

serialVersionID (a field) is not declared as a member the Serializable interface. The field name is merely a convention and the field is accessed by reflection at runtime. It is not possible to use interfaces to define a field contract.

The warning is "compiler magic" that knows it should emit a warning when encountering a type implementing Serializable that does not have such a field. This warning behavior cannot be emulated for other fields in application code alone - obtaining a similar warning requires using a [customized] compiler or lint/code-checker that understands the desired heuristics.

  • Compiler magic, eh? Well, that's certainly not helpful when you need a class to define extra fields to accomodate an interface (say, a weapon interface that extends an item class, which would require the variable ATTACK_NAME to be declared). Any clue on how that could be done instead? – Santo Guevarra Mar 17 '13 at 06:00
  • @SantoGuevarra There are lots of things in Java I don't agree with. But that is secondary to [how the language is defined](http://docs.oracle.com/javase/specs/) and the resulting limitations/approaches. The closest that can be done - short of adding said custom heuristics to a compiler/lint tool - is requiring a certain *method* to be implemented. –  Mar 17 '13 at 06:00
  • I see... Well, I guess that awnsers my question. Thanks anyway! – Santo Guevarra Mar 17 '13 at 06:04