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


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

Re: What qualifies as a constant ? (Was: Reverse sorting an array)

Date 2011-09-30 17:15 +0200
From Mayeul <mayeul.marguet@free.fr>
Newsgroups comp.lang.java.help
Subject Re: What qualifies as a constant ? (Was: Reverse sorting an array)
References <8ea2add1-ce9c-423b-bdb8-92c461cf9c6d@5g2000yqo.googlegroups.com> <0tab87dkpjalvsqde3v720ap8h8pa8l2or@4ax.com> <4e85b9e8$0$1080$426a34cc@news.free.fr> <11444264.0.1317393558767.JavaMail.geo-discussion-forums@prfe27>
Message-ID <4e85dd15$0$27982$426a74cc@news.free.fr> (permalink)
Organization Guest of ProXad - France

Show all headers | View raw


On 30/09/2011 16:39, Lew wrote:
> On Friday, September 30, 2011 5:45:49 AM UTC-7, Mayeul wrote:
>> On 30/09/2011 13:58, Roedy Green wrote:
>>> On Fri, 30 Sep 2011 04:03:01 -0700 (PDT), Fred
>>> <albert.xt...@gmail.com>   wrote, quoted or indirectly quoted
>>> someone who said :
>>>
>>>> import java.util.*;
>>>>
>>>> class InputCounter {
>>>>      public static void main(String[] args) {
>>>> 	final int MAX_NUMBERS = 50;
>>>> 	int[] array = new int[MAX_NUMBERS];
>>>> 	Arrays.sort(array, Collections.reverseOrder());
>>>>      }
>>>> }
>>>
>>> MAX_NUMBERS is not a constant (static final) so should not be
>>> capitalised.  Alternatively, make it a static final.
>>
>> Hum, I'm interested in further explanation.
>> It looks to me like the JLS defines MAX_NUMBERS as a /constant
>> variable/, and since I cannot find anything the JLS would define as just
>> a /constant/, only /constant expressions/ and /constant variables/, It
>> looks to me like a constant variable should qualify to be called a constant.
>>
>> Unless I'm wrong, it seems like a constant to all extent and purpose anyway.
>>
>> Now, does it qualify to be written in all caps, I admit I'm not sure it
>> is the same question. I would use all caps, but then again I'm not all
>> that confident with my naming best practices.
>
> You are correct, Mayeul, wrt the definition of a "constant", which is most explicit in JLS §4.12.4:
> "We call a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression (§15.28) a /constant variable/."
>
> Since the naming convention in question relates to variables, this is the definition that applies.
>
> However, Roedy, you are correct that the convention applies most officially only to class constant variables:
>
> Java Coding Conventions, §9, "Naming Conventions":
> <http://www.oracle.com/technetwork/java/codeconventions-135099.html#367>
> "The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores ("_"). (ANSI constants should be avoided, for ease of debugging.)"
>
> However, you are right, Mayeul, that the convention is often extended to instance constants, or would be except that they don't make any sense.  Whatever can be initialized with a compile-time constant expression (JLS §15.28) shouldn't be repeated across every instance independently anyway.  What's the point?  The value can't differ between instances, so just freaking make it a class constant, and the naming convention question becomes easy.
>
> Another common extension of the convention is to name 'final' immutable instances in all upper case.  That's a horse of a different color.  Now the value can differ between instances if the variable is not static.  But then, that poses a problem for upper-case naming; how "constant" is a thing that can have a different value every time it appears, simply because you have a different instance.  So it really doesn't make sense to name a non-static final reference to an immutable instance with all upper case.
>
> So in the end, Roedy is correct.  The official and largely only sensible convention is to reserve upper-case names for class-level constant variables, or to extend the convention, class-level final pointers to immutable instances.

Thanks, Lew, for the detailed analysis.

However, the example we're speaking about is not using an instance 
constant. It is using a local constant variable. Now, without having 
verified it, I expect such constants are not repeated any more than a 
class constant would be.

I'd expect the point of that is to narrow the variable's scope down to 
one single method. The point of /this/ would be... Well, questionable, 
okay. I do that when I want to give a meaningful name to a constant 
rather an a magical number, but the relevance of it only applies to one 
single method. This usually only happens within classes I feel could be 
refactored more clearly, though.

--
Mayeul

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


Thread

Reverse sorting an array Fred <albert.xtheunknown0@gmail.com> - 2011-09-30 04:03 -0700
  Re: Reverse sorting an array Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-30 07:50 -0400
    Re: Reverse sorting an array Fred <albert.xtheunknown0@gmail.com> - 2011-09-30 15:24 -0700
      Re: Reverse sorting an array Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-30 20:50 -0400
        Re: Reverse sorting an array Roedy Green <see_website@mindprod.com.invalid> - 2011-10-01 19:38 -0700
          Re: Reverse sorting an array Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-10-01 22:58 -0400
            Re: Reverse sorting an array Lew <lewbloch@gmail.com> - 2011-10-01 20:21 -0700
    Re: Reverse sorting an array Fred <albert.xtheunknown0@gmail.com> - 2011-09-30 15:31 -0700
      Re: Reverse sorting an array Roedy Green <see_website@mindprod.com.invalid> - 2011-09-30 15:45 -0700
        Re: Reverse sorting an array Fred <albert.xtheunknown0@gmail.com> - 2011-09-30 16:27 -0700
          Re: Reverse sorting an array Patricia Shanahan <pats@acm.org> - 2011-09-30 16:58 -0700
    Re: Reverse sorting an array Roedy Green <see_website@mindprod.com.invalid> - 2011-09-30 15:43 -0700
      Re: Reverse sorting an array Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-30 20:57 -0400
  Re: Reverse sorting an array Roedy Green <see_website@mindprod.com.invalid> - 2011-09-30 04:58 -0700
    What qualifies as a constant ? (Was: Reverse sorting an array) Mayeul <mayeul.marguet@free.fr> - 2011-09-30 14:45 +0200
      Re: What qualifies as a constant ? (Was: Reverse sorting an array) Lew <lewbloch@gmail.com> - 2011-09-30 07:39 -0700
        Re: What qualifies as a constant ? (Was: Reverse sorting an array) Mayeul <mayeul.marguet@free.fr> - 2011-09-30 17:15 +0200
          Re: What qualifies as a constant ? (Was: Reverse sorting an array) Lew <lewbloch@gmail.com> - 2011-09-30 08:42 -0700

csiph-web