By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,469 Members | 1,885 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,469 IT Pros & Developers. It's quick & easy.

Why are plain getter/setters used?

P: n/a
Hello.

I know a getter can return other thing than a field.
I know a setter can do more things than setting a field.
I know there are "setter only" cases and "getter only" cases.
I do use getters and setters when needed.
But most of getter/setter pairs I have seen are just like below;
private int x;
public int getX() { return x; }
public void setX(int x) { this.x = x; } I cannot understand it is better than below. public int x;

How do you think?

Thank you in advance.
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Liz
It provides a door that seems to be open in your
example, but allows the code owner to close the
door by adding code to the accessor.

"Kiwi" <ki**@s05.itscom.net> wrote in message
news:dc**************************@posting.google.c om...
Hello.

I know a getter can return other thing than a field.
I know a setter can do more things than setting a field.
I know there are "setter only" cases and "getter only" cases.
I do use getters and setters when needed.
But most of getter/setter pairs I have seen are just like below;
private int x;
public int getX() { return x; }
public void setX(int x) { this.x = x; }

I cannot understand it is better than below.
public int x;

How do you think?

Thank you in advance.

Jul 17 '05 #2

P: n/a
ki**@s05.itscom.net (Kiwi) wrote in message news:<dc**************************@posting.google. com>...
Hello.

I know a getter can return other thing than a field.
I know a setter can do more things than setting a field.
I know there are "setter only" cases and "getter only" cases.
I do use getters and setters when needed.
But most of getter/setter pairs I have seen are just like below;
private int x;
public int getX() { return x; }
public void setX(int x) { this.x = x; }

I cannot understand it is better than below.
public int x;

How do you think?

Thank you in advance.

The reason of simple getter/setter is to make room for extension. When
you make a class, you do not know whether a subtype of the class is
needed or not. If you make accesses as a method call, there is a
possibility of overriding the method call in sub class.

Actually, making a routine as a method call is good starting point for
OOP.

Regards,
Minkoo Seo
Jul 17 '05 #3

P: n/a
good question, but the answer goes back to the need to encapsulate, that
is "hide" details concerning the implementation of a given class from
the outside world. coming from a visual basic world, you may find this
to be unnecessary, but it is intended as a measure to reduce complexity
in code.

while having these set/get pairs may seem to be an annoyance, they give
you flexibility at later time to change the standard behavious from
"return x" or "x=" to anything more complicated that is required by
future system requirements. if your class function suddenly has to find
the value of x based on the results from another class, or even
dynamically, then this complication can be resolved by writing a more
appropaite set/get function. supported code that relies on this class
then does not have to be altered....!

know what i mean...

also, in many ways encapsulation phyloposhy comes from the expression
"keep them in the dark and feed them horse manure" also known as the
Mushroom approach.

works great for management too...

- perry
Liz wrote:
It provides a door that seems to be open in your
example, but allows the code owner to close the
door by adding code to the accessor.

"Kiwi" <ki**@s05.itscom.net> wrote in message
news:dc**************************@posting.google.c om...
Hello.

I know a getter can return other thing than a field.
I know a setter can do more things than setting a field.
I know there are "setter only" cases and "getter only" cases.
I do use getters and setters when needed.
But most of getter/setter pairs I have seen are just like below;
private int x;
public int getX() { return x; }
public void setX(int x) { this.x = x; }


I cannot understand it is better than below.
public int x;


How do you think?

Thank you in advance.



Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.