Effective Java 3/E summary chanpter 10 (Shorter version)
19 Apr 2021Chanpter 11 : Serialization
You can find the longer version of this summary here.
Serialization
- Use
JSON,protocol bufferinstead of Java serialization.JSONis human-readable.- Because the
protocol bufferis a binary representation, it is highly efficient.
Prohibit use of Serializable
Serailzableis vulnerable to security- Prohibit
Serailzableinclassesdesigned for inheritance andinterfaces. - Inner classes should not implement serialization.
- If not for inheritance, use serialization proxy pattern
- Create nested class
- Implement Outer class
writeReplace()+readObject() - Implement Nested class
readResolve()
If you’re still going to use Seralizable,
- Judgment of logical and physical differences.
- Logic == physics
- Just implement
Serailzable
- Just implement
- Logic != Physics
- Custom serialization
transient+writeObject()+readObject()- UID explicitly granted (compatibility)
- When writing
readObejct()(like public constructor)- Defensive copy
- Prohibit calling overridable methods
- Instance control class
Enumtype for singleton class- If the
enumtype is not available,readResolve()+transient