There’s a religious war goin on if you look this up at the sun java forums. Some say it’s a matter of preference, while some say they don’t like to use the <class>.<const> format(what!!) but clearly there seemed to be no answer. This javaworld article however seems to me the most clear one and makes me partial to putting them in a class with a private constructor. I also personally like the practice of <classname>.<const>, this generally clarifies the context of the constant
, what this means is that if you have constants for UI and maybe you would specifically say UIConstants.MAX_MESSAGE_LENGTH and in another place you would use NetConstants.MAX_MESSAGE_LENGTH. This is just a personal preference, which I feel nice about
.
What was the best part of this article however was this important bit
If you expose the strings, they get compiled into clients. If you then change them, it has no effect on the clients unless you recompile them. If, on the other hand, you use access methods, the new values immediately propagate to all of the clients.
I had never thought of this
and initially dismissed this as optimization made by an older version of the compiler(the article itself was written in June 2001) but it’s still the case with the 1.4.1 compiler! Another one of those rare gotcha’s