Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2008

Tip: Looking for answers? Try searching our database.

Where do you keep contants in Interfaces or in classes?

Thread view: 
Royan - 30 Mar 2008 17:38 GMT
I have some controversial information on what is the best place to
keep constants, is there any fundamental recommendation?
Stefan Ram - 30 Mar 2008 18:33 GMT
Newsgroups: comp.lang.java.gui,comp.lang.java.programmer
Followup-To: comp.lang.java.programmer

>I have some controversial information on what is the best place to
>keep constants, is there any fundamental recommendation?

 The best place for a question not related to GUI programming
 is outside of »comp.lang.java.gui«.

 The best place for a question in the subject header line is
 a copy in the body of a post.

 (1) Constants can only be kept in interfaces, classes might
 have »final fields« or »constant variables«, but these are not
 called »constants« in Java.

 JLS3, 4.2.4 mentions constants in classes:

     »Other useful constructors, methods, and constants are
     predefined in the classes Float, Double, and Math.«

 I admit that this contradicts my previous statement (1). But
 this might be the only place, where the JLS3 mentions
 »constants in classes«.

 In JLS3, however, 6.8.6 the wording is:

     »The names of constants in interface types should be, and
     final variables of class types may conventionally be, ...«

 This supports (1).

 (BTW, »final variable of class type« seems ambigous to me.

 Is this a final variable /whose type/ is a class, such as:

final java.lang.Object v; ?

 Or is this a final variable that is /a field/ of a class:

class Example { ... final int v; ... } ? - ... Never mind)

 Constant-related terms are also mentioned in JLS3, 8.3.2.1:

     »One subtlety here is that, at run time, static variables
     that are final and that are initialized with compile-time
     constant values are initialized first. This also applies
     to such fields in interfaces (§9.3.1). These variables are
     constants” that will never be observed to have their
     default initial values (§4.12.5), even by devi-ous
     programs. See §12.4.2 and §13.4.9 for more discussion.«

 The definition of an instance of an enum type is called
 an »enum constant« by JLS3, 8.9.

 At least, the BNF-grammar given in the JLS3 only seems to
 mention »constant declaration« for interfaces (JLS3, 9.1.4):

InterfaceMemberDeclaration:
ConstantDeclaration

 This seems to suggest that by the grammar only interfaces
 can have constants (and enumerations can have enum constants).

 I also found this related little-read paragraph from JLS3,
 13.4.10 interesting:

     »The best way to avoid problems with "inconstant
     constants" in widely-distributed code is to declare as
     compile time constants only values which truly are
     unlikely ever to change. Other than for true mathematical
     constants, we recommend that source code make very
     sparing use of class variables that are declared static
     and final. If the read-only nature of final is required,
     a better choice is to declare a private static variable
     and a suitable accessor method to get its value.  [...]

     We also recommend, as a general rule, that only truly
     constant values be declared in interfaces. We note, but do
     not recommend, that if a field of primitive type of an
     interface may change, its value may be expressed
     idiomatically as in:

           interface Flags {
           boolean debug =new Boolean(true).booleanValue();
           }

     insuring that this value is not a constant. Similar idioms
     exist for the other primi-tive types.«

 So now the JLS does not only specify Java, but also acts
 as a style guide.

 By 15.28, a compile-time constant expression does not have to
 be a constant, it might be a name refering to a constant
 variable (4.12.4).

Newsgroups: comp.lang.java.gui,comp.lang.java.programmer
Followup-To: comp.lang.java.programmer


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.