Adding "implements serializable" effects - Java

The cost is close to zero, not worth being concerned about.
Some further details:
  • There is no increase in the size of each object instance
  • There is a small increase in the size of the class itself, but as this is a one-off cost it is trivial when amortised over a large number of instances
  • There may be a slight additional runtime cost for anything that needs to do interface checks at runtime (reflection, instancof lookups, extra pressure on inline caches etc.). Again, this is likely to be negligible for most purposes.
  • Serializable is a marker interface, there are no methods that require to be implemented. Other marker interface examples are: Clonable, SingleThreadModel, Event listener.
-------------------------------------------------------------------------------------

There is no performance impact unless you perform serialization/deserialization but there are trade offs in terms of api design.
From Effective java by Joshua Bloch
  • A major cost of implementing Serializable is that it decreases the flexibility to change a class’s implementation once it has been released
  • A second cost of implementing Serializable is that it increases the likelihood of bugs and security holes
  • A third cost of implementing Serializable is that it increases the testing burden associated with releasing a new version of a class
Upto what extent these are applicable to you depend of your usecase.

Ref: Stackoverflow

Comments

Popular posts from this blog

public vs protected vs default access modifiers - Java

Class, Reference and Object