why use getters/setters?

Discussion in 'Java' started by buu, Sep 8, 2007.

  1. buu

    buu Guest

    can anyone tell me or give me some link?

    I'm newbie in java, and get used to work in .net, and getters/setters
    confuse me a little bit.
     
    buu, Sep 8, 2007
    #1
    1. Advertising

  2. buu wrote:
    > can anyone tell me or give me some link?
    >
    > I'm newbie in java, and get used to work in .net, and getters/setters
    > confuse me a little bit.


    getters & setters is the Java equivalent of .NET properties.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Sep 8, 2007
    #2
    1. Advertising

  3. buu

    Mark Space Guest

    buu wrote:
    > can anyone tell me or give me some link?
    >
    > I'm newbie in java, and get used to work in .net, and getters/setters
    > confuse me a little bit.
    >
    >


    They're standard, in that other APIs expect them and work with them
    already. The GUI, Swing, is based heavily on use of getters and setters.

    Just curious: how would you expect things to work instead? Accessing
    properties through a method is pretty standard software engineering in
    OOD. Java does have the option of using public properties if you really
    want to. The get/set part is just a standardized naming convention.
    (But still useful.)
     
    Mark Space, Sep 8, 2007
    #3
  4. buu wrote:
    > can anyone tell me or give me some link?
    >
    > I'm newbie in java, and get used to work in .net, and getters/setters
    > confuse me a little bit.
    >
    >

    The idea behind {g,s}etters is that it gives the person who writes the
    class a chance to catch all access to a variable. Publicly exposing the
    variable is often a bad thing, and is highly warned against.

    An example of when {g,s}etters are useful would be in a point class:

    class Point {
    private double x, y;
    private double r, theta;

    double getX();
    double getY();
    double getR();
    double getTheta();
    double setX(double x);
    double setY(double y);
    double setR(double r);
    double setTheta(double theta);
    }

    [ Method bodies not shown for conciseness ]

    If the variables were made public, it would be the client's job to
    ensure that the rectangular/polar coordinates were kept in sync. With
    the {g,s}etters, the class can transparently maintain the two variables,
    potentially even removing the r and theta variables.

    In short, {g,s}etters:
    1. allow the class to internally keep related variables in sync.
    2. provide for pseudo-variable support.
    3. allow the internal structure to be modified without affecting the
    public API.
    4. allow the class to keep track of accesses and perform sanity checks.
    5. shift the burden of bookkeeping away from the clients.

    There are some OO languages that even have get/set overloading (e.g.,
    PHP, Python).
    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Sep 8, 2007
    #4
  5. buu

    mich Guest

    "buu" <> wrote in message news:...
    > can anyone tell me or give me some link?
    >
    > I'm newbie in java, and get used to work in .net, and getters/setters
    > confuse me a little bit.


    get and set methods make it easier to make changes in one place.

    private int x;
    public void setX(int parm) {
    this.x = parm;
    }


    what if you need to run some test on the parm, because you want to check for
    something? Example:


    public void setX(int parm) {
    if(parm<0) {
    this.x = 0;
    }
    else {
    this.x = parm;
    }
    }


    so you just add the test in one place, instead of EVERY PLACE where x is
    used.
     
    mich, Sep 8, 2007
    #5
  6. buu

    buu Guest

    tnx a lot to all :)
     
    buu, Sep 8, 2007
    #6
  7. buu

    Patrick May Guest

    "buu" <> writes:
    > can anyone tell me or give me some link?
    >
    > I'm newbie in java, and get used to work in .net, and getters/setters
    > confuse me a little bit.


    Getters and setters are simply a way to control and monitor
    access to the internal state of an object. They tend to be overused
    by many Java programmers. I recommend that you read Joshua Bloch's
    "Effective Java" and take his advice to prefer immutable objects
    wherever possible.

    Regards,

    Patrick

    ------------------------------------------------------------------------
    S P Engineering, Inc. | Large scale, mission-critical, distributed OO
    | systems design and implementation.
    | (C++, Java, Common Lisp, Jini, middleware, SOA)
     
    Patrick May, Sep 9, 2007
    #7
  8. Another use case for getters and setters:

    Following the get/set pattern allows tools to use introspection to
    generate code to get/set properties. This becomes handy, for example,
    in situations where a configuration file consisting of name value
    pairs needs to be tied to state variables in a program. For example,
    an application container can read a configuration file provided by the
    user and generate code "setXXX(xxx)" in a user provided class to set
    the value of XXX read from a properies or xml configuration file.

    On Sep 8, 9:10 pm, Patrick May <> wrote:
    > "buu" <> writes:
    > > can anyone tell me or give me some link?

    >
    > > I'm newbie in java, and get used to work in .net, and getters/setters
    > > confuse me a little bit.

    >
    > Getters and setters are simply a way to control and monitor
    > access to the internal state of an object. They tend to be overused
    > by many Java programmers. I recommend that you read Joshua Bloch's
    > "Effective Java" and take his advice to prefer immutable objects
    > wherever possible.
    >
    > Regards,
    >
    > Patrick
    >
    > ------------------------------------------------------------------------
    > S P Engineering, Inc. | Large scale, mission-critical, distributed OO
    > | systems design and implementation.
    > | (C++, Java, Common Lisp, Jini, middleware, SOA)
     
    M. Ranganathan (NIST), Sep 9, 2007
    #8
  9. buu

    Roedy Green Guest

    Roedy Green, Sep 9, 2007
    #9
  10. buu

    Guest

    On Sep 9, 2:23 am, "M. Ranganathan (NIST)" <> wrote:
    > Another use case for getters and setters:
    >
    > Following the get/set pattern allows tools to use introspection to
    > generate code to get/set properties. This becomes handy, for example,
    > in situations where a configuration file consisting of name value
    > pairs needs to be tied to state variables in a program. For example,
    > an application container can read a configuration file provided by the
    > user and generate code "setXXX(xxx)" in a user provided class to set
    > the value of XXX read from a properies or xml configuration file.


    Not that I advocate having public non-final ivars, but such a tool
    could in theory parse those as "properties" as well.
     
    , Sep 9, 2007
    #10
  11. buu

    Patrick May Guest

    "M. Ranganathan (NIST)" <> writes:
    > On Sep 8, 9:10 pm, Patrick May <> wrote:
    >> "buu" <> writes:
    >> > can anyone tell me or give me some link?

    >>
    >> > I'm newbie in java, and get used to work in .net, and getters/setters
    >> > confuse me a little bit.

    >>
    >> Getters and setters are simply a way to control and monitor
    >> access to the internal state of an object. They tend to be overused
    >> by many Java programmers. I recommend that you read Joshua Bloch's
    >> "Effective Java" and take his advice to prefer immutable objects
    >> wherever possible.

    >
    > Another use case for getters and setters:
    >
    > Following the get/set pattern allows tools to use introspection to
    > generate code to get/set properties.


    [ Please don't top post. ]

    Relying on a convention to achieve appropriate results from
    introspection is unreliable and unnecessary. The introspection
    mechanism can look at the member variables and constructors without
    those clues.

    The problem with making accessors and mutators available for
    introspection is that they are then also available, and will be used
    by, other programmers.

    Regards,

    Patrick

    ------------------------------------------------------------------------
    S P Engineering, Inc. | Large scale, mission-critical, distributed OO
    | systems design and implementation.
    | (C++, Java, Common Lisp, Jini, middleware, SOA)
     
    Patrick May, Sep 9, 2007
    #11
    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. Berlin Brown
    Replies:
    5
    Views:
    929
    Darryl L. Pierce
    Jan 25, 2004
  2. George Armhold
    Replies:
    1
    Views:
    1,466
    Chris Riesbeck
    Sep 2, 2004
  3. Getters and Setters

    , Jun 9, 2006, in forum: Java
    Replies:
    7
    Views:
    692
    Eddy Young
    Jun 10, 2006
  4. Raaijmakers, Vincent (IndSys,GE Interlogix)

    Newbie in win32com: getters and setters

    Raaijmakers, Vincent (IndSys,GE Interlogix), Oct 23, 2003, in forum: Python
    Replies:
    0
    Views:
    283
    Raaijmakers, Vincent (IndSys,GE Interlogix)
    Oct 23, 2003
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,137
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page