• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Object Constants

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All
Just a quick question on constants in java. I know from looking at the coding standards that primitive constants should be declared in upper-case, as in:
public static int NUM_FIELDS = 10;
However, it (the standards) doesnt seem to mention object constants. I noticed from the sun course material (SL-275, "Java Programming Language") that object constants CAN use mixed-case letters. I seem to remember the course tutor specifying that you SHOULD use mixed-case letters, as in:
public static String fieldName = "field";
Has anyone got a definitive answer on this?
Many Thanks
Jamie
[ May 28, 2003: Message edited by: Jamie Orme ]
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jamie,
They are not constants unless they are final. Your examples show static variables initialized to literals. My sentiments on naming all constants (static final) is to use all upper case letters and underscores for spaces whether they be objects or primitives.
 
Jamie Orme
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Michael
Sorry, forgot to put in 'final' keyword, my mistake!!! Its been a long day....
Thanks for that, has anyone else got any ideas on this?
Thanks
Jamie
[ May 28, 2003: Message edited by: Jamie Orme ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sure "final" is necessary to be constant, though maybe not sufficient. Sun's coding conventions say we should use UPPERCASE_WITH_UNDERSCORES for "ANSI constants", without bothering to define what this means. I did a web search but no luck; any info would be appreciated. My guess, however, is that they mean final variables initialized with compile-time constant expressions. This would include String literals and expressions, but nothing initialized with a method call or a non-constant variable. So
are constants, but

are not. When JDK 1.5 comes out, enum values might also qualify as constants.
Of course, others here may well disagree...
[ May 28, 2003: Message edited by: Jim Yingst ]
 
Jamie Orme
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All
Thanks for the replies so far. The course material says the following (just located it):
"Constants - Primitive constants should be all uppercase with the word separated by underscores. Object constants can use mixed-case letters.
HEAD_COUNT
MAXIMUM_SIZE"
Rather unhelpfully it doesn�t give an example for Object constants. However I seemed to have scribbled my own, indicating that the lecturer prefers the mixed-case solution.
I don�t want to bang on about this, but I would just like to make sure that I follow the majority in the Java community on this one, at least for my assignment!
Thanks again, and all comments welcome.
Jamie
 
reply
    Bookmark Topic Watch Topic
  • New Topic