War das Buch mit dem Titel Emergent Entwurf und zum Thema Singleton lesen ...java verschachtelte Klasse mit statischen Initialisierer
public class MyApp {
private class SingletonHolder {
public Object singleton;
static {
singleton = new Object();
}
}
}
dann eclipse hält über die statische beschweren {}
Java liest, Klassen soll in der Lage sein, multi statische Initialisierung zu haben. Also, wie kann ich die obige Arbeit machen?
/tmp/jc_4873/MyApp.java:5: non-static variable singleton cannot be referenced from a static context
singleton = new Object();
^
/tmp/jc_4873/MyApp.java:4: inner classes cannot have static declarations
static {
^
2 errors
public class MyApp {
private class SingletonHolder {
public static Object singleton;
static {
singleton = new Object();
}
}
}
/tmp/jc_8488/MyApp.java:3: inner classes cannot have static declarations
public static Object singleton;
^
/tmp/jc_8488/MyApp.java:4: inner classes cannot have static declarations
static {
^
2 errors
public class MyApp {
private static class SingletonHolder {
public static Object singleton;
static {
singleton = new Object();
}
}
}
+1 zustimmen und ich werde auch hinzufügen, dass SingletonHolder wahrscheinlich eine statische Klasse sein sollte. –