During deserialization, are subclasses guaranteed to be initialized before any class instance objects are restored from the input stream? Specifically, I have a subclass with static fields that I'd like to access in the superclass readObject()
method. Can I count on the subclass static fields being valid?
Asked
Active
Viewed 185 times
1

Slava Vedenin
- 58,326
- 13
- 40
- 59

ags
- 719
- 7
- 23
-
If you *mention* any class in your code it it guaranteed to be initialized *before your code runs.* Nothing to do with serialization specifically. – user207421 Dec 03 '19 at 05:04
1 Answers
1
I am not an expert on BNF, but the protocol spec for serialization
classDescInfo:
classDescFlags fields classAnnotation superClassDesc
shows that superclass information comes after class information. So it looks like you can rely on your subclass static fields.

Neil Masson
- 2,609
- 1
- 15
- 23
-
1Correct answer, misleading reasoning. Even if the protocol was defined differently, you are always guaranteed to have an initialized class, before an instance is created (unless you are creating the instance in the class initializer itself). – Holger Jun 30 '16 at 10:22