Java PMD warning on non-transient class member

Now I get it.

After adding this definition:

private boolean someFlag;

...it is clear what happens here:

This error message does refer to the accessing schema. PMD states that classes referred to by beans, must also follow the bean schema.

Most likely to support property-style access like MyBean.referredClass.someFlag will be translated to someObject.getReferredClass().getSomeFlag()

PMD it expects that there is a isSomeFlag/getSomeFlag and setSomeFlag method by which you could access its value, and not access it directly.

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

transient is used as a hint to the jvm serialization that it should ignore it when writing a class in a stream/to disk. so if your instance is recovered and becomes an object in memory, the field will be null.

the problem with static members is, there is only one of them in memory at a time. so its not entirely clear what should happen upon deserialization. should the old value be kept? or the cached version overwrite the old one?

what you should do: do not use static fields in Serializable classes at all. move it somewhere else, or better yet, do not use static members / singletons at all. they introduce a global state which may lead to numerous problems and bad OO design.


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".


See the rule that is happening here

BeanMembersShouldSerialize

If a class is a bean, or is referenced by a bean directly or indirectly it needs to be serializable. Member variables need to be marked as transient, static, or have accessor methods in the class. Marking variables as transient is the safest and easiest modification. Accessor methods should follow the Java naming conventions, i.e.if you have a variable foo, you should provide getFoo and setFoo methods.