Java PMD warning on non-transient class member

Yuval Adam picture Yuval Adam · Feb 26, 2009 · Viewed 26.3k times · Source

On line:

private boolean someFlag;

I get the following PMD warning:

Found non-transient, non-static member. Please mark as transient or provide accessors.

Can someone please explain why this warning is there and what it means? (I understand how to fix it, I don't understand why it's there...)

I'm getting this on many other member declarations as well...


EDIT: My class is definitely not a bean, and not serializable...

Answer

tcurdt picture tcurdt · Feb 26, 2009

I assume your class is a bean that by definition implements Serializable. A transient variable will be excluded from the serialization process. If you serialize and then deserialize the bean the value will be actually have the default value.

PMD assumes you are dealing with a serializable bean here. For a bean it is expected to have getters/setters for all member variables. As you have omitted these, you imply that your member variable is not part of the bean ....and so does not need to be serialized. If that is the case you should exclude it from the serialization. Which you do by marking the variable as "transient".