2

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?

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
banjokaboom
  • 134
  • 2
  • 16

4 Answers4

7

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

Java enumerations vs. static constants

Best practice for global constants involving magic numbers

Community
  • 1
  • 1
leonbloy
  • 73,180
  • 20
  • 142
  • 190
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

JohnnyO
  • 3,018
  • 18
  • 30
1

Generally we use it to hold constants value.

For example:

public static final String USER_ADMIN="admin";

Also See

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
0

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.

Bhushan
  • 18,329
  • 31
  • 104
  • 137