How to Implement Constants in Java

What is the best way to implement constants in Java?

That is perfectly acceptable, probably even the standard.

(public/private) static final TYPE NAME = VALUE;

where TYPE is the type, NAME is the name in all caps with underscores for spaces, and VALUE is the constant value;

I highly recommend NOT putting your constants in their own classes or interfaces.

As a side note: Variables that are declared final and are mutable can still be changed; however, the variable can never point at a different object.

For example:

public static final Point ORIGIN = new Point(0,0);

public static void main(String[] args){

ORIGIN.x = 3;

}

That is legal and ORIGIN would then be a point at (3, 0).

How to declare a constant in Java?

  1. You can use an enum type in Java 5 and onwards for the purpose you have described. It is type safe.
  2. A is an instance variable. (If it has the static modifier, then it becomes a static variable.) Constants just means the value doesn't change.
  3. Instance variables are data members belonging to the object and not the class. Instance variable = Instance field.

If you are talking about the difference between instance variable and class variable, instance variable exist per object created. While class variable has only one copy per class loader regardless of the number of objects created.

Java 5 and up enum type

public enum Color{
RED("Red"), GREEN("Green");

private Color(String color){
this.color = color;
}

private String color;

public String getColor(){
return this.color;
}

public String toString(){
return this.color;
}
}

If you wish to change the value of the enum you have created, provide a mutator method.

public enum Color{
RED("Red"), GREEN("Green");

private Color(String color){
this.color = color;
}

private String color;

public String getColor(){
return this.color;
}

public void setColor(String color){
this.color = color;
}

public String toString(){
return this.color;
}
}

Example of accessing:

public static void main(String args[]){
System.out.println(Color.RED.getColor());

// or

System.out.println(Color.GREEN);
}

How do you define a class of constants in Java?

Use a final class, and define a private constructor to hide the public one.

For simplicity you may then use a static import to reuse your values in another class

public final class MyValues {

private MyValues() {
// No need to instantiate the class, we can hide its constructor
}

public static final String VALUE1 = "foo";
public static final String VALUE2 = "bar";
}

in another class :

import static MyValues.*
//...

if (VALUE1.equals(variable)) {
//...
}

Best practice for Constant Class in Java

Answer to your questions :

Naming Scheme is correct? -

Ideally you should not create class with names like Variables, Parameters, etc. as these names also has literal meanings with them in many languages. Besides that, these classes are going to store Constants! You can simply create single class with name Constants! No need to create different Constant classes till it's absolutely necessary. Perhaps, in that case you should go for enums. Why? Check these links :

  • Link 1.

  • Link 2

Constant class structure is among best practices ?

This is answered in above point. Keep your naming convention readable enough so that you won't require separate constant classes.
Always try to implement KISS(Keep it Simple Stupid) and DRY(Don't repeat yourself!)

Should I create separate constant classes or having them encapsulated in a parent Constant class is fine?

Ideally not. If you need such requirement then go for enums. As enums are typesafe, you wont end up creating misleading statements. For ex.
If you write following potentially incorrect code then Constant class won't mind:

String playerType = Constants.MALE;

But, if you use enums, that would end up as:

// Compile-time error - incompatible types!
PlayerType playerType = Gender.MALE;


Related Topics



Leave a reply



Submit