Getter/Setter - Serialization

Discussion in 'Java' started by Thomas Lehmann, Jul 24, 2011.

  1. Hi,

    probably a silly question. I have a class and some members will be
    initialized only when calling a 'create' method. I'm using Eclipse and
    there I get a warning "Found non-transient, non-static member. Please
    mark as transient or provide accessors."

    My question is now how to handle it correct. When I provide a setter
    (assume a creation date) then I could set the creation date to another
    as initialized when calling 'create'. That's why I wouldn't provide a
    setter for this member. But obviously the warning want to tell me that
    I might have problems with the serialization then obviously requiring
    that the setter exists (and yes I'm intending to do serialization -
    probably XML).

    Could you please give some helpful comments on this?
    Thomas Lehmann, Jul 24, 2011
    #1
    1. Advertising

  2. Thomas Lehmann

    markspace Guest

    On 7/24/2011 8:07 AM, Thomas Lehmann wrote:
    > I might have problems with the serialization then obviously requiring
    > that the setter exists



    Yeah, I think the setter/getter might be required for XML style
    serialization, but not in the general case.

    What are you doing that Eclipse says you need to mark a field as
    transient? This doesn't make sense to me, because general serialization
    will handle this just fine. I think I need some context here.

    Here's a good article on serialization:

    <http://java.sun.com/developer/technicalArticles/Programming/serialization/>
    markspace, Jul 24, 2011
    #2
    1. Advertising

  3. The warning message comes with PMD (plugin).
    I'm intending to use XStream for XML serialization and so far I can
    see
    there is no need to provide getter and setter.

    I know that article (URL) but reading also "Effective Java" you will
    be warned
    about the problem with using "Serializable".


    On 24 Jul., 17:30, markspace <-@.> wrote:
    > On 7/24/2011 8:07 AM, Thomas Lehmann wrote:
    >
    > > I might have problems with the serialization then obviously requiring
    > > that the setter exists

    >
    > Yeah, I think the setter/getter might be required for XML style
    > serialization, but not in the general case.
    >
    > What are you doing that Eclipse says you need to mark a field as
    > transient?  This doesn't make sense to me, because general serialization
    > will handle this just fine.  I think I need some context here.
    >
    > Here's a good article on serialization:
    >
    > <http://java.sun.com/developer/technicalArticles/Programming/serializa...>
    Thomas Lehmann, Jul 24, 2011
    #3
  4. Thomas Lehmann

    markspace Guest

    On 7/24/2011 9:33 AM, Thomas Lehmann wrote:
    > The warning message comes with PMD (plugin). I'm intending to use
    > XStream for XML serialization and so far I can see there is no need
    > to provide getter and setter.



    I haven't used XStream; if you don't think there's a problem, I'd
    ignore the PMD plug-in and just do the right thing. PMD is likely in error.


    > I know that article (URL) but reading also "Effective Java" you will
    > be warned about the problem with using "Serializable".



    I don't have time to review EJ right now, care to refresh my memory what
    is pertinent in EJ to this problem?
    markspace, Jul 24, 2011
    #4
  5. Thomas Lehmann

    lewbloch Guest

    On Jul 24, 8:07 am, Thomas Lehmann
    <> wrote:
    > Hi,
    >
    > probably a silly question. I have a class and some members will be
    > initialized only when calling a 'create' method. I'm using Eclipse and
    > there I get a warning "Found non-transient, non-static member. Please
    > mark as transient or provide accessors."
    >
    > My question is now how to handle it correct. When I provide a setter
    > (assume a creation date) then I could set the creation date to another
    > as initialized when calling 'create'. That's why I wouldn't provide a
    > setter for this member. But obviously the warning want to tell me that


    Did you declare the attribute as 'final'? If you want to protect a
    member variable from change, that's often the best way. It has other
    benefits as well.

    > I might have problems with the serialization then obviously requiring
    > that the setter exists (and yes I'm intending to do serialization -
    > probably XML).
    >
    > Could you please give some helpful comments on this?


    Could you please give us an SSCCE?
    http://sscce.org/

    As markspace points out, we need some context here.

    To grok serialization, you should rigorously study the material in
    Joshua Bloch's seminal book /Effective Java/, which thoroughly covers
    the risks and best practices involved.
    http://java.sun.com/docs/books/effective/
    Chapter 11

    --
    Lew
    lewbloch, Jul 24, 2011
    #5
  6. Thomas Lehmann

    Arne Vajhøj Guest

    On 7/24/2011 12:33 PM, Thomas Lehmann wrote:
    > On 24 Jul., 17:30, markspace<-@.> wrote:
    >> On 7/24/2011 8:07 AM, Thomas Lehmann wrote:
    >>
    >>> I might have problems with the serialization then obviously requiring
    >>> that the setter exists

    >>
    >> Yeah, I think the setter/getter might be required for XML style
    >> serialization, but not in the general case.
    >>
    >> What are you doing that Eclipse says you need to mark a field as
    >> transient? This doesn't make sense to me, because general serialization
    >> will handle this just fine. I think I need some context here.
    >>
    >> Here's a good article on serialization:
    >>
    >> <http://java.sun.com/developer/technicalArticles/Programming/serializa...>

    > The warning message comes with PMD (plugin).
    > I'm intending to use XStream for XML serialization and so far I can
    > see
    > there is no need to provide getter and setter.
    >
    > I know that article (URL) but reading also "Effective Java" you will
    > be warned
    > about the problem with using "Serializable".


    Yes.

    But his arguments against serialization applies to xstream as
    well, so ...

    Arne
    Arne Vajhøj, Jul 25, 2011
    #6
  7. Thomas Lehmann

    Roedy Green Guest

    On Sun, 24 Jul 2011 08:07:06 -0700 (PDT), Thomas Lehmann
    <> wrote, quoted or indirectly
    quoted someone who said :

    >Hi,
    >
    >probably a silly question. I have a class and some members will be
    >initialized only when calling a 'create' method. I'm using Eclipse and
    >there I get a warning "Found non-transient, non-static member. Please
    >mark as transient or provide accessors."


    Is this field for some reason non-serialisable? If it is serialisable,
    you could just ignore the warning.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    Most of computer code is for telling the computer
    what do if some very particular thing goes wrong.
    Roedy Green, Jul 25, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raoul Markus
    Replies:
    3
    Views:
    1,077
    Roedy Green
    Sep 20, 2003
  2. Timo Nentwig
    Replies:
    75
    Views:
    3,295
    Jon A. Cruz
    Jan 18, 2004
  3. Stacey
    Replies:
    1
    Views:
    1,781
    Collin VanDyck
    Feb 10, 2004
  4. Timo Nentwig

    EL: accessing non-getter/setter

    Timo Nentwig, May 10, 2004, in forum: Java
    Replies:
    6
    Views:
    636
    Murray
    May 11, 2004
  5. Adam Sandler

    problem with getter and setter not working

    Adam Sandler, May 25, 2006, in forum: ASP .Net
    Replies:
    12
    Views:
    721
    Jimi200478
    May 26, 2006
Loading...

Share This Page