Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.42!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder2-2.proxad.net!nx01.iad01.newshosting.com!newshosting.com!news-out.readnews.com!transit3.readnews.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.help Subject: Re: What qualifies as a constant ? (Was: Reverse sorting an array) Date: Fri, 30 Sep 2011 07:39:18 -0700 (PDT) Organization: http://groups.google.com Lines: 76 Message-ID: <11444264.0.1317393558767.JavaMail.geo-discussion-forums@prfe27> References: <8ea2add1-ce9c-423b-bdb8-92c461cf9c6d@5g2000yqo.googlegroups.com> <0tab87dkpjalvsqde3v720ap8h8pa8l2or@4ax.com> <4e85b9e8$0$1080$426a34cc@news.free.fr> Reply-To: comp.lang.java.help@googlegroups.com NNTP-Posting-Host: 24.6.43.78 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1317393649 16318 127.0.0.1 (30 Sep 2011 14:40:49 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 30 Sep 2011 14:40:49 +0000 (UTC) In-Reply-To: <4e85b9e8$0$1080$426a34cc@news.free.fr> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=24.6.43.78; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.help:1165 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 > > wrote, quoted or indirectly quoted > > someone who said : > > > >> import java.util.*; > >> > >> class InputCounter { > >> public static void main(String[] args) { > >> final int MAX_NUMBERS =3D 50; > >> int[] array =3D 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. >=20 > Hum, I'm interested in further explanation. > It looks to me like the JLS defines MAX_NUMBERS as a /constant=20 > variable/, and since I cannot find anything the JLS would define as just= =20 > a /constant/, only /constant expressions/ and /constant variables/, It=20 > looks to me like a constant variable should qualify to be called a consta= nt. >=20 > Unless I'm wrong, it seems like a constant to all extent and purpose anyw= ay. >=20 > Now, does it qualify to be written in all caps, I admit I'm not sure it= =20 > is the same question. I would use all caps, but then again I'm not all=20 > 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": "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. --=20 Lew What's the upper-case character for eszett?