객체의 생성을 제어해야 한다면?
Singleton 디자인 패턴
class SingletonClass {
private static SingletonClass instance = new SingletonClass();
private SingletonClass() {} // 기본 생성자를 private로..
public static SingletonClass getInstance() {
return instance;
}
public class SingletonTest {
public static void main (String[] args) {
SingletonClass sc1 = SingletonClass.getInstance();
SingletonClass sc2 = SingletonClass.getInstance(); // sc1의 객체와 같다
}
}