Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

why use getters/setters?

Thread view: 
buu - 08 Sep 2007 18:47 GMT
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.
Arne Vajhøj - 08 Sep 2007 19:01 GMT
> 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
Mark Space - 08 Sep 2007 19:15 GMT
> 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.)
Joshua Cranmer - 08 Sep 2007 20:00 GMT
> 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).
Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

mich - 08 Sep 2007 20:23 GMT
> 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.
buu - 08 Sep 2007 22:59 GMT
tnx a lot to all :)
Patrick May - 09 Sep 2007 02:10 GMT
> 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.
         pjm@spe.com  | (C++, Java, Common Lisp, Jini, middleware, SOA)
M. Ranganathan (NIST) - 09 Sep 2007 07:23 GMT
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.

> > can anyone tell me or give me some link?
>
[quoted text clipped - 15 lines]
>                        | systems design and implementation.
>           p...@spe.com  | (C++, Java, Common Lisp, Jini, middleware, SOA)
nebulous99@gmail.com - 09 Sep 2007 09:03 GMT
> Another use case for getters and setters:
>
[quoted text clipped - 5 lines]
> 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.
Patrick May - 09 Sep 2007 16:53 GMT
>> > can anyone tell me or give me some link?
>>
[quoted text clipped - 11 lines]
> 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.
         pjm@spe.com  | (C++, Java, Common Lisp, Jini, middleware, SOA)
Roedy Green - 09 Sep 2007 08:36 GMT
>I'm newbie in java, and get used to work in .net, and getters/setters
>confuse me a little bit.

see http://mindprod.com/jgloss/getter.html
http://mindprod.com/jgloss/setter.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.