This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String constants

 
Wickes Potgieter
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • 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...
 
Hanna Habashy
Ranch Hand
Posts: 532
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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;
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Julian Kennedy
Ranch Hand
Posts: 823
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic