Java constant definition

How to manage constants collectively in Java There are three types that you know other than properties. Which one should be implemented? Story

  1. Constant interface
  2. Constant class
  3. enum class

Use an enum. I've heard that it's defined in the interface, but I've never seen it.

Example of implementing a constant interface (quoted from Effective Java 2nd Edition Item 19)
public interface PhysicalConstants {
    //Avogadro constant (1/mol)
    static final double AVOGADROS_NUMBER = 6.02214199e23;
    //Boltzmann constant (J/K)
    static final double BOLTZMANN_CONSTANT = 1.3806503e-23;
    //Electron mass (kg)
    static final double ELECTRON_MASS = 9.10938188e-31;
}

I have never seen it. .. Does it mean that if the number of constant interfaces increases, it will be implemented as much as necessary? The name seems to be covered. .. High degree of coupling. It's not how to use the interface.

Example of implementing a constant class
public final class Constants {
    public static final String PATH_SEP = "/";
    public static final String LINE_SEP = "¥n";
    private Constants (){}
}

If the person who makes it first changes the class name to Constants, it will become bloated. It's almost like that.

Example of implementing in enum class
public enum ExamEnum {
    SUNDAY(0),
    MONDAY(1),
    TUESDAY(3),
    WEDNESDAY(4),
    THURSDAY(5),
    FRIDAY(6),
    SATURDAY(7);
}
 

Someday I started using it because it was called "constant class (laughs)". Compared to others, there are no disadvantages other than the amount of code to be implemented, so I continue to use it. Once you write the amount of code, you can do basic things with copy and paste. .. I won't write it here because there are many good ways to use it.

Recommended Posts

Java constant definition
Constant definition location guideline
Java class definition and instantiation
[Java] Make it a constant
Java
Java Servlet 4.0 web.xml schema definition
Java learning (0)
Studying Java ―― 3
[Java] array
[Java] Annotation
[Java] Module
Java array
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
java (array)
Java static
Java serialization
JAVA paid
enum definition
Java (set)
java shellsort
Studying Java -5
[Java ~ Variable definition, type conversion ~] Study memo
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
Java features
java beginner 3
Java memo
java (encapsulation)
Java inheritance
[Java] Overload
Java basics
Decompile Java
[Java] Annotation
java notes
java beginner
Java (add2)
JAVA (Map)
[java] interface
Java basics
Java methods
Java diary
[Java] enum
[Java] FizzBuzzCounter
Studying Java ―― 8