Was reading the book called Emergent Design and on the topic of singleton ...
public class MyApp {
private class SingletonHolder {
public Object singleton;
static {
singleton = new Object();
}
}
}
then eclipse keeps complaining about the static {}
Was reading Java, classes supposed to be able to have multi static initializer. So how can I make the above work ?
/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();
}
}
}