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


Groups > comp.lang.java.help > #2051 > unrolled thread

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

Started byTimothy Madden <terminatorul@gmail.com>
First post2012-08-22 14:47 +0300
Last post2012-08-23 08:58 -0700
Articles 7 — 5 participants

Back to article view | Back to comp.lang.java.help


Contents

  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

#2051 — Why "Static field should be accessed in a static way" ?

FromTimothy Madden <terminatorul@gmail.com>
Date2012-08-22 14:47 +0300
SubjectWhy "Static field should be accessed in a static way" ?
Message-ID<5034c6c8$0$282$14726298@news.sunsite.dk>
Hello

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.

Is there some reason why tools like Eclipse claim the field should be
accessed in a static way ?

Thank you,
Timothy Madden

[toc] | [next] | [standalone]


#2052

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-08-22 08:45 -0400
Message-ID<k12k9j$k7m$1@dont-email.me>
In reply to#2051
On 8/22/2012 7:47 AM, Timothy Madden wrote:
> Hello
>
> 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.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#2053

FromLew <noone@lewscanon.com>
Date2012-08-22 06:31 -0700
Message-ID<k12n04$af8$1@news.albasani.net>
In reply to#2052
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

[toc] | [prev] | [next] | [standalone]


#2055

Frommarkspace <-@.>
Date2012-08-22 08:54 -0700
Message-ID<k12vba$mlm$1@dont-email.me>
In reply to#2051
On 8/22/2012 4:47 AM, Timothy Madden wrote:
> Hello
>
> To my knowledge, using non-static access even for static fields/methods
> is good practice.

No, where the heck did you hear this? Do you have a source you can cite? 
  Seriously, if there's some site spreading misinformation I'd like a 
link to it.

> It allows for later changing the field into and
> instance field..

First, English grammar: look it up.  Second, refactoring is easy.

> It also allows for using that field without knowing
> much wheather it is static or not.

That's the problem: anyone reading your code won't know that it's a 
static member.  Also, learn to spell.

>
> Is there some reason why tools like Eclipse claim the field should be
> accessed in a static way ?

Because it's best practice.


[toc] | [prev] | [next] | [standalone]


#2056

FromTimothy Madden <terminatorul@gmail.com>
Date2012-08-22 21:05 +0300
Message-ID<50351f7d$0$293$14726298@news.sunsite.dk>
In reply to#2055
On 08/22/2012 06:54 PM, markspace wrote:
> On 8/22/2012 4:47 AM, Timothy Madden wrote:
>> Hello
>>
>> To my knowledge, using non-static access even for static fields/methods
>> is good practice.
> 
> No, where the heck did you hear this? Do you have a source you can cite?
>  Seriously, if there's some site spreading misinformation I'd like a
> link to it.

Sorry, I don't have a link, I guess it was just my idea.
But I get the point.

Thank you,
Timothy Madden

[toc] | [prev] | [next] | [standalone]


#2061

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-08-23 09:44 -0700
Message-ID<cfnc38tg6la6djo4dh2954sdatk7sfdbha@4ax.com>
In reply to#2056
On Wed, 22 Aug 2012 21:05:41 +0300, Timothy Madden
<terminatorul@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>
>Sorry, I don't have a link, I guess it was just my idea.
>But I get the point.

Javac's warning message is definitive.
-- 
Roedy Green Canadian Mind Products http://mindprod.com
A new scientific truth does not triumph by convincing its opponents and making them see the light,
but rather because its opponents eventually die, and a new generation grows up that is familiar with it.
~ Max Planck 1858-04-23 1947-10-04 

[toc] | [prev] | [next] | [standalone]


#2060

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-08-23 08:58 -0700
Message-ID<njkc38h0i0blbg2iq20d9ki0mpulchtnga@4ax.com>
In reply to#2051
On Wed, 22 Aug 2012 14:47:12 +0300, Timothy Madden
<terminatorul@gmail.com> wrote, quoted or indirectly quoted someone
who said :

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

I think most programmers would disagree with you.  Doing it your way
creates a misleading impression that the method affects fields of that
object specially. Since IDEs can make global changes, motivations on
making things easier to change later are less and less important in
favour of making programs clearer to read now.

-- 
Roedy Green Canadian Mind Products http://mindprod.com
A new scientific truth does not triumph by convincing its opponents and making them see the light,
but rather because its opponents eventually die, and a new generation grows up that is familiar with it.
~ Max Planck 1858-04-23 1947-10-04 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.help


csiph-web