본 포스팅은 면접을 위한 CS전공지식 노트를 읽고 정리한 글입니다.
class SingletonInstace{
private static final SingletonInstace INSTACE = new SingletonInstace();
public static SingletonInstace getINSTACE() {
return INSTACE;
}
}
public class Singleton {
public static void main(String[] args) {
SingletonInstace a = SingletonInstace.getINSTACE();
SingletonInstace b = SingletonInstace.getINSTACE();
System.out.println("a : " + a.hashCode());
System.out.println("b : " + b.hashCode());
}
}
out(hashcode는 각 환경마다 다름)
a : 1118140819
b : 1118140819
a와 b의 인스턴스는 같다는 걸 확인할 수 있다.