Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This might seem like an odd question, but I'm worried that I'm putting too many things as constants/finals at the top of my java class. I've started to put every value that is in my program into a constant (At least the ones that influence it's overall effect). The reason I'm doing it is because then it's easy to find the values and change them latter, even if they are only used once, it's easier to find them at the top. Is this a bad or good practice, I really have no idea.

Here's an example:

private static final Color COLOR_SELECTED = Color.BLUE;
private static final Color COLOR_ALTERNATE_ONE = Color.WHITE;
private static final Color COLOR_ALTERNATE_TWO = new Color(240, 240 , 240);
private static final Color TEXT_COLOR = Color.black;
share|improve this question
1  
COLOR_SELECTED doesn't read like a constant at first glance (sounds like a variable I.E. what is the currently selected color or something)... maybe you should rename it to SELECTED_TEXT_COLOR or whatever it really means. –  Esailija May 25 '13 at 12:20
    
@Esailija Thanks, I will. –  Mike G May 25 '13 at 20:42

2 Answers 2

up vote 3 down vote accepted

No, this is not a bad thing. Your code will be able to refer to TEXT_COLOR and consistently get Color.black. Even if you only use it once now, you might use it more than once as your code continues to develop.

An even better thing would be to read these values from a properties file, so you could change them without recompiling. But that's only something to worry about if you think the colours used might actually change in the future.

share|improve this answer

This is a good practice! This is equivalent to the idea of removing magic numbers.

In the future, if you want to change the color of something that is selected, you can do it in one place. Trying to look for every instance of Color.BLUE might not be incorrect if a different item also uses the color blue.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.