Results 1 to 2 of 2

Thread: Do you use the transient keyword?

  1. #1

    Default Do you use the transient keyword?

    Hi,

    I found this PMD code checker plugin for eclipse, pretty cool you may want to check it out, http://pmd.sourceforge.net/

    I ran it on my projects here, and one violation that is showing up a lot is,

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

    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,
    marked as 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.

    private transient int someFoo;//good, it's transient
    private static int otherFoo;// also OK
    private int moreFoo;// OK, has proper accessors, see below
    private int badFoo;//bad, should be marked transient

    do i need to do that? I'd be typing transient a good bit, i have a lot of spring beans that reference other beans, i.e.

    Code:
    public class SomeUtil  {
        private SomethingManager somethingManager;
        public void setSomethingManager(somethingManager)  {
            this.somethingManager = somethingManager;
        }
    }
    will anything bad happen if somethingManager is not transient?

  2. #2
    Join Date
    Oct 2004
    Location
    Herndon, VA, US
    Posts
    648

    Default

    I don't agree with this rule. Technically whether a bean (or any POJO for that matter) is serializable or not depends solely on whether it implements Serializable, which is orthogonal to whether and how a bean chooses to expose some of its properties.

    In other words, I could have a "badFoo" that is completely internal, and yet must be persisted.
    --Jing Xue

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •