How to manage constants collectively in Java There are three types that you know other than properties. Which one should be implemented? Story
Use an enum. I've heard that it's defined in the interface, but I've never seen it.
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.
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.
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