This post originated from an RSS feed registered with Java Buzz
by Ben Hosking.
Original Post: Constants in Java, is it right to use them?
Feed Title: A Funny Java Flavoured Look at the World
Feed URL: http://businesslogs.com/WebLog/RSS.xml
Feed Description: The blog looks at using Java and programming in general as it pops up in life as a Java programmer. It will have links to interesting Java articles and resources. It will also have a lot of SCJP Java 1.5 information and links as I am currently studying
I have recently been thinking about constants and in particular static final constants. I know it is frowned upon in the OO world but sometimes I need to put a two strings in a few places
which I have called
/** a constant value for a blank String. */
public static final String BLANK = "";
/** a constant value for a blank space String. */
public static final String BLANKSPACE = " ";
These strings are often put inbetween values in error messages, initializing some string variables etc. As I found I kept needing them in a number of classes I thought I might as well put them in a constants class and just call them from there. So I have two questions really.
1. What are the alternatives to doing this? Does Java have these constants I was looking at String and Character but I couldn't find anything.
2. Is it wrong for my classes to have a dependency on this constants class just for the a blank space and a blank.
What do other people do? Basically I just got fed up of making BLANK constants after checkstyle kept moaning at me