views:

1935

answers:

4

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

A: 

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.

Andreas Petersson
+1  A: 

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.

toolkit
+1  A: 

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

tcurdt
OK I get it, but the class is NOT a bean... PMD is making irrelevant assumptions on my back :)
Yuval A
+1  A: 

ok, now i get it. after adding the 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**.
Andreas Petersson