Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java PMD warning on non-transient class member

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

like image 841
Yuval Adam Avatar asked Feb 26 '09 09:02

Yuval Adam


2 Answers

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

like image 141
tcurdt Avatar answered Sep 20 '22 17:09

tcurdt


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**. 
like image 45
Andreas Petersson Avatar answered Sep 22 '22 17:09

Andreas Petersson