Is it possible to have final transient
fields that are set to any non-default value after serialization in Java? My usecase is a cache variable — that\'s why i
The short answer is "no" unfortunately - I've often wanted this. but transients cannot be final.
A final field must be initialized either by direct assignment of an initial value or in the constructor. During deserialization, neither of these are invoked, so initial values for transients must be set in the 'readObject()' private method that's invoked during deserialization. And for that to work, the transients must be non-final.
(Strictly speaking, finals are only final the first time they are read, so there are hacks that are possible that assign a value before it is read, but for me this is going one step too far.)