Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #1168
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Newsgroups | comp.lang.java.help |
| Subject | Re: What qualifies as a constant ? (Was: Reverse sorting an array) |
| Date | 2011-09-30 08:42 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <4033162.609.1317397344126.JavaMail.geo-discussion-forums@prib32> (permalink) |
| 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> <4e85dd15$0$27982$426a74cc@news.free.fr> |
On Friday, September 30, 2011 8:15:55 AM UTC-7, Mayeul wrote: ... > 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. It is repeated every time the method is invoked, semantically. Operationally you'd expect a constant load, of course. But my issue here is semantic. If it's a constant, as in this example, the value cannot change between invocations or instances, so why scope it so narrowly? One could argue as you do: > 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. and I agree, particularly that the point is questionable. More significantly it isn't consistent with convention and most style recommendations. If something is a compile-time constant, it /should/ be declared at the class level, with static final constant variables. Part of this is for maintenance - compile-time constants' class dependency rules differ from other variables (even final immutable ones that aren't compile-time constants). You also get early assignment of 'final' member variables (including non-constant ones). These are technical advantages for using true class-level constants, which though perhaps slight still combine with convention to trump a style argument for the questionable practice. In summary: - Upper-case names are limited to class-level constant variables by the core convention. - Compile-time constant variables are treated differently from other variables, and such member variables particularly differently. - The convention is to declare such constants as class member variables. - Build and maintenance considerations support that convention for pragmatic reasons. - Following the convention models the problem semantics more correctly. -- Lew
Back to comp.lang.java.help | Previous | Next — Previous in thread | Find similar
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