why use getters/setters?

B

buu

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.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

buu said:
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
 
M

Mark Space

buu said:
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.)
 
J

Joshua Cranmer

buu said:
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).
 
M

mich

buu said:
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.
 
P

Patrick May

buu said:
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
 
M

M. Ranganathan (NIST)

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.
 
N

nebulous99

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.
 
P

Patrick May

M. Ranganathan (NIST) said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top