Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #6965

Re: Getters and Setters

From Arved Sandstrom <asandstrom3minus1@eastlink.ca>
Newsgroups comp.lang.java.programmer
Subject Re: Getters and Setters
References <79c811c0-8eec-4c87-8027-2bc971818278@l9g2000prd.googlegroups.com> <j1su1a$upa$1@dont-email.me>
Message-ID <zgs0q.26773$kI4.21909@newsfe02.iad> (permalink)
Organization Public Usenet Newsgroup Access
Date 2011-08-10 06:34 -0300

Show all headers | View raw


On 11-08-10 12:30 AM, Eric Sosman wrote:
> On 8/9/2011 9:59 PM, Vikram wrote:
>> Why do we use getter and setters if we can do the same work by
>> declaring the public data members.
>> Your answer will be appriciable.
[ SNIP ]
> 
>     "Getter" and "setter" are ordinarily used to refer to methods
> that don't do much work: A "setter" usually stores its argument
> value in a data field (perhaps with a few sanity checks and perhaps
> changing a related field to maintain agreement), and a "getter"
> typically just returns whatever is stored in some field.  They may
> remain this way -- but the fact that they are methods and that the
> underlying data is hidden from outsiders gives you the freedom to
> change things later, if the need arises.  If you expose the data
> fields, those fields' names, types, and significance all become part
> of your class' API, something you can't change without risking
> breakage in the code that uses your class.
> 
To add to this, the typical question that a class designer ought to have
shouldn't be Vikram's, but rather whether to have public accessors.

With rare exceptions (like ubiquitous constants) we strongly prefer
private fields, for reasons you pointed out nicely. Since the arguments
you make apply just as much to logic intra-class, it also makes sense to
supply _private_ getters and setters if an "interesting" method of a
class needs to access a field. In effect the rest of the class, with
respect to a given field, is also an "outsider" - class cohesion is
maintained; maintainability/modifiability is greatly enhanced.

The real _first_ question should be what ought to be publicly accessible
at all. _If_ public access is merited it should be through methods; I'd
be pleased if a future language change made it impossible to have
instance fields as anything but "private".

AHS

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Getters and Setters Vikram <vpradhan33@gmail.com> - 2011-08-09 18:59 -0700
  Re: Getters and Setters "John B. Matthews" <nospam@nospam.invalid> - 2011-08-09 22:11 -0400
  Re: Getters and Setters Arne Vajhøj <arne@vajhoej.dk> - 2011-08-09 22:37 -0400
  Re: Getters and Setters Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-08-09 23:30 -0400
    Re: Getters and Setters Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-08-10 06:34 -0300
  Re: Getters and Setters Roedy Green <see_website@mindprod.com.invalid> - 2011-08-09 21:50 -0700

csiph-web