File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes General Computing and the fly likes Where to store constants? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » General Computing
Bookmark "Where to store constants?" Watch "Where to store constants?" New topic

Where to store constants?

Anjana Ravindran
Ranch Hand

Joined: Aug 22, 2005
Posts: 76
I really doubt where to store the constants according to a good programming design.

Here is my situation. i have an email address and my application has an email functionality to send out the email using this email address.

Right now, this email address is used in only one place.

1. Can i store this email address in the code file where i am using?
2. Can i have one common code file to have all the constants used by the application?
3. Can i have a table called 'key_values' table which will store values in an array list format
eg: Key Value
sender_role admin,user,guest

please let me know which option would be good.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
While the three options you mention are all possibilities, for configuration options that rarely (if ever) change -like email addresses- I'd use a properties file, and make its contents available to all classes of an application. That could happen either through a static method in some class, or through a config object that each class has access to.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33130

This forum is for "operating systems, applications, hardware, drivers, features, etc. General computer use stuff." I'm going to move it to one of our coding forums for you.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: Where to store constants?
It's not a secret anymore!