Re: What qualifies as a constant ? (Was: Reverse sorting an array)
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 consta=
nt.
Unless I'm wrong, it seems like a constant to all extent and purpose anyw=
ay.
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 =A74.12.4:
"We call a variable, of primitive type or type String, that is final and in=
itialized with a compile-time constant expression (=A715.28) a /constant va=
riable/."
Since the naming convention in question relates to variables, this is the d=
efinition that applies.
However, Roedy, you are correct that the convention applies most officially=
only to class constant variables:
Java Coding Conventions, =A79, "Naming Conventions":
<http://www.oracle.com/technetwork/java/codeconventions-135099.html#367>
"The names of variables declared class constants and of ANSI constants shou=
ld be all uppercase with words separated by underscores ("_"). (ANSI consta=
nts should be avoided, for ease of debugging.)"
However, you are right, Mayeul, that the convention is often extended to in=
stance constants, or would be except that they don't make any sense. Whate=
ver can be initialized with a compile-time constant expression (JLS =A715.2=
8) shouldn't be repeated across every instance independently anyway. What'=
s the point? The value can't differ between instances, so just freaking ma=
ke it a class constant, and the naming convention question becomes easy.
Another common extension of the convention is to name 'final' immutable ins=
tances in all upper case. That's a horse of a different color. Now the va=
lue 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 co=
nvention is to reserve upper-case names for class-level constant variables,=
or to extend the convention, class-level final pointers to immutable insta=
nces.
--
Lew
What's the upper-case character for eszett?