I have a java class that implements serializable and I'm assuming the variable within the class would also be serialized but SonarQube is complaining to me that it is not.
My snippet of code is shown below:
It'll throw a NotSerializableException when you try to Serialize it. To avoid that, make that field a transient field.
If you don't want to serialize otherProperties , then the field should be marked as transient : private transient Map<String, Object> otherProperties; Otherwise, you can change the type of otherProperties to an implementation of Map that implements Serializable , such as HashMap .
transient is a variables modifier used in serialization. At the time of serialization, if we don't want to save value of a particular variable in a file, then we use transient keyword. When JVM comes across transient keyword, it ignores original value of the variable and save default value of that variable data type.
transient is a Java keyword which marks a member variable not to be serialized when it is persisted to streams of bytes. When an object is transferred through the network, the object needs to be 'serialized'. Serialization converts the object state to serial bytes.
SonarQube marked this line as an error, because java.util.List doesn't implement java.io.Serializable. java.util.ArrayList is serializable, but the bondAxeMarkQuoteUpdates
is protected
so somebody can assign other non-serializable list to it (e.g. in a subclass).
To solve the problem you can:
transient
, but it will be ignored during serializationprivate
, so SonarQube can verify that nobody assigned non-serializable list to itIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With