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


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

Re: Java variable access

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Java variable access
Date 2019-04-12 10:56 -0400
Organization A noiseless patient Spider
Message-ID <q8q8uk$4ua$1@dont-email.me> (permalink)
References <109a7224-f392-46c3-bf56-7d8bf2bfb4bf@googlegroups.com>

Show all headers | View raw


On 4/12/2019 9:57 AM, Eric Douglas wrote:
> If one would need to get and set values of simple class variables, is it better to declare public variables or declare them private with getter/setter methods, or are there situations where you would do both?  I have been declaring all variables private though sometimes creating the getter/setter methods just seems like unnecessary busy work if making them public would accomplish the same thing.  Advantages to making them private would include 1) deprecating the getter/setter if the variable at some point is no longer needed, 2) customizing the getter/setter if you change the class to require special code to execute every time they set a value, and 3) changing the variable name without affecting any code trying to get/set the value.

     It is often the case that a field should not be set to "just any
old value," but only to a non-negative value, or to a non-null value,
or to a value consistent with that of some other field, or ...  If
such is the case (or if such might ever become the case, which means
"pretty much always"), a setter method is the suitable vehicle for
checking and enforcing the constraints.  Make the field public or
protected (or even package-private), and you'll have a very hard time
figuring out how a "must be a power of two" field got set to twelve.

     It is often the case that a class "owns" a mutable object that
must only be mutated by the class itself and not by unknown outside
agencies.  In such a case both the setter and getter methods can
make defensive copies, giving far more security than a Javadoc comment
saying "The array returned by this method must not be modified, please,
oh, please please please."

     It is sometimes the case that a field is set in a constructor or
at class initialization and is never changed thereafter.  In such a
case a `public final' field would be defensible.  `Boolean.FALSE' is
fine, but `Boolean.getFalse()' would just be silly.

     To your point (1), this seldom happens unless the purpose of the
class changes rather radically.  Yes, variables come and go as the
implementation changes -- but if the variable reflects some essential
part of the class' API, that part almost certainly reappears in a new
form.  (Consider a "getter" that does not simply retrieve a stored
value, but computes what it returns.)  If a variable *and its purpose*
both vanish, it may be time to publish a new class.

-- 
esosman@comcast-dot-net.invalid
Six hundred forty-nine days to go.

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


Thread

Java variable access Eric Douglas <e.d.programmer@gmail.com> - 2019-04-12 06:57 -0700
  Re: Java variable access Arne Vajhøj <arne@vajhoej.dk> - 2019-04-12 10:06 -0400
    Re: Java variable access Eric Douglas <e.d.programmer@gmail.com> - 2019-04-12 07:23 -0700
      Re: Java variable access Arne Vajhøj <arne@vajhoej.dk> - 2019-04-12 10:35 -0400
      Re: Java variable access Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-04-12 10:58 -0400
        Re: Java variable access Eric Douglas <e.d.programmer@gmail.com> - 2019-04-12 08:31 -0700
          Re: Java variable access Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-04-12 14:40 -0400
        Re: Java variable access Arne Vajhøj <arne@vajhoej.dk> - 2019-04-12 12:31 -0400
  Re: Java variable access Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-04-12 10:56 -0400
  Re: Java variable access Joerg Meier <joergmmeier@arcor.de> - 2019-04-12 23:11 +0200
    Re: Java variable access Arne Vajhøj <arne@vajhoej.dk> - 2019-04-12 21:51 -0400
      Re: Java variable access Joerg Meier <joergmmeier@arcor.de> - 2019-04-13 21:59 +0200
        Re: Java variable access Arne Vajhøj <arne@vajhoej.dk> - 2019-04-13 18:38 -0400
          Re: Java variable access Joerg Meier <joergmmeier@arcor.de> - 2019-04-16 22:08 +0200
            Re: Java variable access Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-04-16 16:46 -0400
              Re: Java variable access Joerg Meier <joergmmeier@arcor.de> - 2019-04-16 23:01 +0200
              Re: Java variable access Eric Douglas <e.d.programmer@gmail.com> - 2019-04-17 06:42 -0700
                Re: Java variable access Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2019-04-17 21:35 +0200
                Re: Java variable access Eric Douglas <e.d.programmer@gmail.com> - 2019-04-17 14:05 -0700
                Re: Java variable access Joerg Meier <joergmmeier@arcor.de> - 2019-04-19 10:40 +0200
                Re: Java variable access Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-04-19 17:20 -0400

csiph-web