Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constants

 
Abhijith Prabhakar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to declare a file which contains all the constants of my project. Could you guys tell me shall i declare all the constants in a Class or an Interface? and Why?
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Given the information you provide: that depends on your problem.

/Svend Rost
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest that you use a class. The class can be package private when you don't want the constants to be seen outside the package. The constant interface pattern (usage of interfaces for just declaring constants) should be avoided.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd resist doing all of the constants for a system in one place. You'll make it hard to reuse any subset of your system without dragging that whole constants class along.

You can usually find them a home along with the classes that really need them. For example, if you have a set of constants for log message severity they probably belong on the Logger class or in the logger package.

Also look into Enums. In Java 5 and later they can replace some constants like those severity levels in nifty ways.
[ March 07, 2007: Message edited by: Stan James ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic