I'm reviewing some code from my job from another employee and came across a class consisting of only public static final fields. What is the benefit of this, and how would it be used? My guess is that it makes it easy to retrieve info from XML tags. Any other ideas or knowledge?
-
It depends on the type of each item in the class, but if they are all of the same type then it could likely be made into an `enum`. – pickypg May 26 '11 at 17:50
-
Deleted the XML tag, as this question has nothing to do with XML. – Michael Kay May 26 '11 at 22:39
4 Answers
It's a common Java idiom to define (sort of) constants, to avoid harcoding fixed ('magic' - probably duplicated and hard to refactor) values in code.
https://softwareengineering.stackexchange.com/questions/59642/best-practices-for-constants
http://www.javapractices.com/topic/TopicAction.do?Id=2
Its a common (albeit bad) way in Java to have a collection of constant values for one reason or another. Also, if its older Java code, it was a common way to implement enums before there was language support for them

- 3,018
- 18
- 30
-
What would be a better way? Not all constants form sets of related values that would be implemented as enums. – Michael Borgwardt May 26 '11 at 17:50
Generally we use it to hold constants value.
For example:
public static final String USER_ADMIN="admin";
Also See
It is used for holding constants which will be required by single/multiple source files.
public static final String username="admin";
But to achieve this, I would rather use interface
, instead of class. Since any class can implement it, it will help more in my purpose. Although this choice depends on requirements.

- 18,329
- 31
- 104
- 137