This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Defining class constants Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Defining class constants" Watch "Defining class constants" New topic
Author

Defining class constants

Barry Brashear
Ranch Hand

Joined: Jun 05, 2001
Posts: 303
Can someone tell me how a class constant should be defined? Is it okay to
have a constant defined in the same file that it is used? Or should all
constants be in a class by themselves?

Thanks.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
As usual, it depends on the situation. Some constants are values that you are allowed to pass into a particular method. Then it makes sense for the class that has the method to also have the constants. Some constants are used all over the system. Then it makes sense to make them more global in their own class.

It's a neat goal to have only one reason to change a class. That steers you away from a single class with all the constants in the system because it would have many reasons to change.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Defining class constants
 
Similar Threads
String Variables/ literals
A minor confusion about constants..
Whats Better? aving all constants in an interfce or having a separate constants file?
String constants
ANSI Constants