2017-04-25 4 views
0

모두 읽어 주셔서 감사합니다. 제네릭을 이해하려고 노력 중입니다. 제네릭 매개 변수를 사용하여 싱글 톤을 만드는 곳입니다.일반 싱글 톤에서 정적 필드 사용하기

public class Singleton<T> { 
public static T getInstance() { 
if (instance == null) 
instance = new Singleton<T>(); 
return instance; 
} 
private static T instance = null; 
} 

하지만 난이 오류가 발생했습니다 :

내가 해결 방법으로 무엇을 사용할 수있는 비 정적 타입 T에 대한 정적 참조를 만들 수 있습니까? 또는 더 나은 아직, 오류의 원인은 무엇입니까? newaccts에서

+0

. 하나만 가질 수 있으므로 다른 기본 유형에 대해 서로 다른 점이 무엇입니까? –

답변

0

봐는 this 게시물에 답변 : 그것은 어쨌든 일반적인 싱글을 가지고 이해가되지 않습니다

You can't use a class's generic type parameters in static methods or static fields. The class's type parameters are only in scope for instance methods and instance fields. For static fields and static methods, they are shared among all instances of the class, even instances of different type parameters, so obviously they cannot depend on a particular type parameter.