I’m using a simple struct where I store readonly values (like properties with no setter in C#). To achieve this I’m using public final int test=42;.
For some reasons I want to allow to serialize this class. I’m using this code:
private void writeObject(ObjectOutputStream oos) throws IOException {
oos.writeInt(test);
}
private void readObject(ObjectInputStream ois) throws IOException {
test=ois.readInt();
}
The last one does not work because the field test is final how can I solve this issue?
You can use reflection to modify the value of
testThe above assumes that your application has the permissions given by the
SecurityManagerto use reflection. If it does not, theSecurityManagerwill throw an exception.