This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I have created two java.files but have a lot of constants. I am thinking that it would be better if I put all my constants in one file and access this file to read the values. This would give me three files:-
Class1.java Class2.java Constants.java (note: I havent actually named my classes Class1 and 2, this is just for examples sake)
How do I access the values of the constant file Do I use import Constants.java at the top of each of the other two files? Do I instantiate the file also
Is creating a constants file good programing practice? [ August 09, 2004: Message edited by: Steve Durber ]
Originally posted by Jeroen Wenting: data members in interfaces ARE generally considered BAD (tm) practice.
Not generally enough that I wouldn't disagree...
Seriously, as long as you don't use it in published API's, implementing interfaces for sharing constants is quite convenient and doesn't do much harm. With Tiger I would use static imports instead, of course...
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Mar 11, 2002
No harm, but confusing. Why should a class implement constants? It should just use the constants. Isn't it?
I'd also make the class final since all it does is define constants. I wouldn't want someone else to extend it and add some behaviour to it (just because the only reason for it to exist is to define constants). Just a personal style.
Just summing up all the replies above, you could simply do the following:
1. Define a new class and declare it to be final. 2. Put in all your constants and declare each one of them to be public and static. 3. Access any constant that you want by using the ClassName.CONSTANT_NAME syntax.
Don't put your constants in an interface. Put them in a final class (so that no one else can extend it like Sadanand mentioned).
Vijayendra <br /> <br />"The harder you train in peace, the lesser you bleed in war"
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: How do I use a file to hold all my constants