*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes String constants Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "String constants" Watch "String constants" New topic
Author

String constants

Wickes Potgieter
Ranch Hand

Joined: Apr 05, 2002
Posts: 68
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...


Wickes Potgieter<br />SCJP<br />BEA Certified Specialist: Server, Integration, Portal, Tuxedo<br />BEA WebLogic Server 7 Certified Enterprise Developer<br />BEA Certified Architect<br /><a href="http://www.bea.com" target="_blank" rel="nofollow">www.bea.com</a>
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
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.


SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
Wickes Potgieter
Ranch Hand

Joined: Apr 05, 2002
Posts: 68
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

Joined: Aug 20, 2003
Posts: 532
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

Joined: Apr 05, 2002
Posts: 68
Thanks Hanna,

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

Thanks,
Wickes
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
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

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Aug 02, 2004
Posts: 823
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
 
subject: String constants