Java-JVM class initialization

public class Test {
    protected static final Logger logger = LoggerFactory.getLogger(Test.class);

    public static void main(String[] args) {
        Singleton singleton = Singleton.getInstance();
        logger.info("counter1:" + singleton.counter1);
        logger.info("counter2:" + singleton.counter2);
    }
}

class Singleton {
    private static Singleton singleton = new Singleton();
    public static int counter1;
    public static int counter2 = 0;

    private Singleton() {
        counter1++;
        counter2++;
    }

    public static Singleton getInstance() {
        return singleton;
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325346011&siteId=291194637