Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Constants

Status
Not open for further replies.

Ladyhawk

Programmer
Jan 22, 2002
534
AU
Hi I am relatively new to Java. I am writting an app that maintains various types of resources (eg books, magazines etc). I have a main class which handles the main window and main navigation of the system and a Resource class. I am using String variables as constants but I have to set up the constants in both the Resource class and the main class. From the main window, the user selects to add a particular resource, so I pass the appropriate constant to the Resource class. Is there a better way of handling the constants? Ladyhawk. [idea]
** ASP/VB/Java Programmer **
 
Hmmm... you can keep constants in a properties file, which you can get in code with a Properties object (check the JDK).

What do you do with your book and magazine references? Are you calling those constants? Be sure to keep those in a separate file from the program properties.
 
Another way you could also make your constants accessible to multiple cllasses is by implementing an interface that contains all of your constants. By definition, all constants in an interface are public static final and are accessible by all classes that implement the interface.
 
crufty... that sounds like a pretty heavy way of going about it... putting the properties in a text file allows you to change global variables without having to change source code and recompile.
 
The app allows the user to add, edit and delete resources. The constants are just used so that I know which resource the resource class is to do stuff for. Ladyhawk. [idea]
** ASP/VB/Java Programmer **
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top