May I do data type conversion within getter/setter?

Discussion in 'Java' started by Raoul Markus, Sep 20, 2003.

  1. Raoul Markus

    Raoul Markus Guest

    Hi,

    I had to change the type of in private variable (from java.util.Date to
    java.sql.Date). May I do type conversions within the getters and setters to
    let the outside interface stay untouched?
    i.e.

    private java.sql.Date mDate;

    public java.util.Date getDate() {
    return new java.util.Date(mDate.getTime());
    }
     
    Raoul Markus, Sep 20, 2003
    #1
    1. Advertising

  2. Raoul Markus

    Juha Laiho Guest

    Raoul Markus <> said:
    >I had to change the type of in private variable (from java.util.Date to
    >java.sql.Date). May I do type conversions within the getters and setters to
    >let the outside interface stay untouched?


    Yep; that (being able to modify the class internals without needing to
    modify anything outside the class) is one of the reasons you have getters
    and setters instead of just providing public access to the variables.
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
     
    Juha Laiho, Sep 20, 2003
    #2
    1. Advertising

  3. Raoul Markus wrote:
    > Hi,
    >
    > I had to change the type of in private variable (from java.util.Date to
    > java.sql.Date). May I do type conversions within the getters and setters to
    > let the outside interface stay untouched?
    > i.e.
    >
    > private java.sql.Date mDate;
    >
    > public java.util.Date getDate() {
    > return new java.util.Date(mDate.getTime());
    > }
    >

    That's one of the best reasons to use getters/setters. When the
    internal data needs to change but you don't want the external API to change.
     
    David Zimmerman, Sep 20, 2003
    #3
  4. Raoul Markus

    Roedy Green Guest

    On Sat, 20 Sep 2003 19:38:26 +0200, Raoul Markus <>
    wrote or quoted :

    >I had to change the type of in private variable (from java.util.Date to
    >java.sql.Date). May I do type conversions within the getters and setters to
    >let the outside interface stay untouched?


    yes. Further there is nothing to do.

    you can write:

    private java.sql.Date d;

    public java.util.Date getDate()
    {
    return d;
    }

    Since java.sql.Date is a subclass of java.util.Date.

    Most people would not even mind if you wrote:

    public java.sql.Date getDate()
    {
    return d;
    }

    Since they can use the result as a java.util.Date without conversion.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Sep 20, 2003
    #4
    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. Timo Nentwig
    Replies:
    75
    Views:
    3,350
    Jon A. Cruz
    Jan 18, 2004
  2. Stacey
    Replies:
    1
    Views:
    1,892
    Collin VanDyck
    Feb 10, 2004
  3. Timo Nentwig

    EL: accessing non-getter/setter

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

    problem with getter and setter not working

    Adam Sandler, May 25, 2006, in forum: ASP .Net
    Replies:
    12
    Views:
    776
    Jimi200478
    May 26, 2006
  5. gamehack
    Replies:
    2
    Views:
    503
    James Stroud
    Jul 18, 2007
Loading...

Share This Page