📄 SingletonService
package hello.core.singleton;
public class SingletonService {
private static final SingletonService instance = new SingletonService();
public static SingletonService getInstance() {
return instance;
}
private SingletonService() {
}
public void logic() {
System.out.println("싱글톤 객체 로직 호출");
}
}
이렇게 되면 해당 클래스가 생성될 때, 자기 자신을 참조값으로 하여 객체를 생성한다.
static 영역에 객체 인스턴스를 미리 하나 생성해서 올려둔다.
이 객체 인스턴스가 필요하면 오직 getInstance() 메서드를 통해서만 조회한다.
딱 1개의 인스턴스만 존재해야하므로 생성자를 private 로 막아서 외부 생성 차단
<aside>
💡 same 과 equal
same → 객체 자체 비교equal → 객체 내 값 비교</aside>