• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

String constants

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

what should I do with String constants for things like button labels, dialog messages, error messages, etc.?

Should I:
1) Put all constants in a properties file and read them at runtime?
2) Declare them as constants at the top of my class?

Any other suggestions?

Thanks,
Wickes...
 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wickes,
Your program should run without the .properties file. At startup, the program should search for .properties file, if it doesn't exist, creat one.
Hence, you cannot rely on .properties to supply you with constants.
The only solution, as I see it, is to hard code them in a constant class.
 
Wickes Potgieter
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Hanna,

what will be the best approach then? Constants defined in the class that uses them or a separate constant class for all other classes?

Thanks,
Wickes
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wickes,
In my project, I created different constant classes. Almost one Constant class for each package, and a Constatnt class in a the utility packeg which contains constants used application wide.
There are some constants are used only within the package, because of that I put them in a constant class for this package only. Constatants I found used among various piecies of the application, I put them in a utility constant class.
 
Wickes Potgieter
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Hanna,

how did you implement the constants class if I may ask?

Thanks,
Wickes
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wickes,
Constant class is a class which contains only constants. No methods and no contructors. It is a class with final static constants, Strings, Integers..etc
You access this constants by typing <class name>.<constants name>
for example:
class ErrorConstants
{
final static String FIRST_ERROR = "My first Message";
}

you access by typing

ErrorConstants.FIRST_ERROR;
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Hanna Habashy:
[...] Hence, you cannot rely on .properties to supply you with constants. The only solution, as I see it, is to hard code them in a constant class.

Not quite. If the properties are read only, you can package a .properties file in your jar and load it up using Class.getResourceAsStream() or ClassLoader.getResourceAsStream(). E.g.Will load up "Foo.properties" from the same directory where "Foo.class" lives. Although by no means required, it's never a bad idea to move user interface text out of Java classes altogether. Such text is often quite volatile, and will be a huge boon if i18n ever becomes an issue (obviously irrelevant for the assignment though).

- Peter
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I personally think that constants classes are overkill unless your constants are used in more than one class, when they're pretty much essential. They lead to rather awkward looking syntax until the new static import arrives in Java 1.5.

Jules
 
reply
    Bookmark Topic Watch Topic
  • New Topic