Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I use a file to hold all my constants

 
Steve Durber
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rajan Chinna
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
data members in interfaces ARE generally considered BAD (tm) practice.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No harm, but confusing.
Why should a class implement constants? It should just use the constants. Isn't it?
 
Steve Durber
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats fantastic thanks to you all for replying
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic