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


Groups > comp.lang.java.help > #2053

Re: Why "Static field should be accessed in a static way" ?

From Lew <noone@lewscanon.com>
Newsgroups comp.lang.java.help
Subject Re: Why "Static field should be accessed in a static way" ?
Date 2012-08-22 06:31 -0700
Organization albasani.net
Message-ID <k12n04$af8$1@news.albasani.net> (permalink)
References <5034c6c8$0$282$14726298@news.sunsite.dk> <k12k9j$k7m$1@dont-email.me>

Show all headers | View raw


Eric Sosman wrote:
> Timothy Madden wrote:
>> To my knowledge, using non-static access even for static fields/methods
>> is good practice. It allows for later changing the field into and
>> instance field.. It also allows for using that field without knowing
>> much wheather it is static or not.
>
>      It seems to me that it would be difficult to make effective
> use of a field without knowing whether it was instance-specific
> or class-specific.
>
>      (CAUTION: Weak Analogy Ahead)
>
>      In the matter of the State v Madden, the Court ruled
>
>      a) The defendant Madden is guilty of making a Gawdawful racket
>         in the middle of the night, and is fined $100 for disturbing
>         the peace, or
>
>      b) The law against racket-eering (sorry) is an unconstitutional
>         abridgement of free speech, and is hereby struck from the books.
>
> One of these judgements is relevant to a particular Person instance
> (Madden must pay the fine), while the other affects every Person
> (nobody will ever have to pay such a fine).  It seems to me there's
> a fundamental difference, and that the Court would have to be aware
> of that difference in issuing a ruling.  Similarly, when you use a
> field you need to understand whether its value is specific to one
> instance or is a common characteristic shared by all.  Ask someone
> for his name and you expect to hear "Timothy Madden," not "Homo
> Sapiens."
>
>      The same considerations lead me to doubt that "later changing
> the field into an instance field" is something one would be likely
> to do.  It's certainly possible to remove static fields and introduce
> instance fields, but I'd argue that this can't be done blindly or
> mechanically.
>
>> Is there some reason why tools like Eclipse claim the field should be
>> accessed in a static way ?
>
>      Probably because the toolmakers think instance-ish access to
> non-instance data is a Java misfeature.

Reference to a static member via an instance is misleading, albeit legal.

As Eric says, it's important when reading source to know which member is 
instancey and which is staticky. The coding standards give us a quick visual 
distinction between a class name and a variable or method name, thus a quick 
visual distinction between static references and instance references.

It can also be difficult to know in which type a static member originates. 
Best practice is to use the type name in which the member is defined.

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

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


Thread

Why "Static field should be accessed in a static way" ? Timothy Madden <terminatorul@gmail.com> - 2012-08-22 14:47 +0300
  Re: Why "Static field should be accessed in a static way" ? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-22 08:45 -0400
    Re: Why "Static field should be accessed in a static way" ? Lew <noone@lewscanon.com> - 2012-08-22 06:31 -0700
  Re: Why "Static field should be accessed in a static way" ? markspace <-@.> - 2012-08-22 08:54 -0700
    Re: Why "Static field should be accessed in a static way" ? Timothy Madden <terminatorul@gmail.com> - 2012-08-22 21:05 +0300
      Re: Why "Static field should be accessed in a static way" ? Roedy Green <see_website@mindprod.com.invalid> - 2012-08-23 09:44 -0700
  Re: Why "Static field should be accessed in a static way" ? Roedy Green <see_website@mindprod.com.invalid> - 2012-08-23 08:58 -0700

csiph-web