File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How do I use a file to hold all my constants Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do I use a file to hold all my constants" Watch "How do I use a file to hold all my constants" New topic
Author

How do I use a file to hold all my constants

Steve Durber
Greenhorn

Joined: Aug 05, 2004
Posts: 9
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 ]
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
SD: How do I access the values of the constant file

If your Constants.java has the following constant,

Access it from other classes like this



SD: Do I use import Constants.java at the top of each of the other two files?
Yes. Import it as you would normally do for the other files. If you are using Java 1.5 look for static imports.

SD: Do I instantiate the file also
Not needed. I will make the constructer private to avoid people instantiating it.

SD: Is creating a constants file good programing practice?
Don't know. But I think it is not a bad practice either!
[ August 10, 2004: Message edited by: Mani Ram ]

Mani
Quaerendo Invenietis
Rajan Chinna
Ranch Hand

Joined: Jul 01, 2004
Posts: 320
I think you can also follow this method.
Create a class interface(Constants.java) and declare all your constants. And implement this in your Class1 & Class2 java files.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
data members in interfaces ARE generally considered BAD (tm) practice.


42
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
No harm, but confusing.
Why should a class implement constants? It should just use the constants. Isn't it?
Steve Durber
Greenhorn

Joined: Aug 05, 2004
Posts: 9
Thats fantastic thanks to you all for replying
Sadanand Murthy
Ranch Hand

Joined: Nov 26, 2003
Posts: 382
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.


Ever Existing, Ever Conscious, Ever-new Bliss
Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I use a file to hold all my constants