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.
constant. It is using a local constant variable. Now, without having
class constant would be.
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
single method. This usually only happens within classes I feel could be
refactored more clearly, though.