From the Serialization chapter of Effective Java:
If the class of an object being deserialized defines a readResolve method with the proper declaration, this method is invoked on the newly created object after it is deserialized.
The example provided in Effective Java is the following and it seems like the throws
part is missing from what the Javadoc says is the correct format ie ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;
// readResolve for instance control - you can do better!
private Object readResolve () {
// Return the one true Elvis and let the garbage collector
// take care of the Elvis impersonator.
return INSTANCE;
}
What is meant by proper declaration here? Will the class even compile if the declaration is not proper? I am finding the sentence a little bit confusing.
From the javadoc of
Serializable
So
proper declaration
means the method must have the same declaration as above.This behavior exists because
But doesn't have any abstract methods declared of its own.