In my quest to develop a generic singleton class, I encountered this problem:
[weiqi@gao] $ cat Singleton.java
public class Singleton<T> {
private static T instance;
public static T getInstance() {
if (instance == null) {
instance = new T();
}
return instance;
}
}
[weiqi@gao] $ javac Singleton.java
Singleton.java:2: non-static class T cannot be referenced from a static context
private static T instance;
^
Singleton.java:3: non-static class T cannot be referenced from a static context
public static T getInstance() {
^
Singleton.java:5: non-static class T cannot be referenced from a static context
instance = new T();
^
3 errors