private 생성자 사용
특정 생성자 호출을 방지한다
java tip
기본 생성자 호출을 방지하는 것이 필요한 경우에 사용하는 기본적인 기법이다.
이런 클래스의 대표적인 예는 java.lang.Math.
소스 코드를 읽어보면 기본 생성자에 private가 붙어 있음을 알 수 있다.
public final class Math {
private Math() {}
public static final double E = 2.7182818284590452354;
public static final double PI = 3.14159265358979323846;
public static double sin(double a) {
return StrictMath.sin(a); // default impl. delegates to StrictMath
}
public static double cos(double a) {
return StrictMath.cos(a); // default impl. delegates to StrictMath
}
public static double tan(double a) {
return StrictMath.tan(a); // default impl. delegates to StrictMath
}
/* ... */
}
당연히 다음과 같이 new 키워드로 private 생성자를 호출하면 에러가 발생한다.
Math m = new Math();