Singleton

package com.question;

public class Singleton {
    
    private static class Holder {
        private static final Singleton singleton = new Singleton();
    }
    
    private Singleton() {
        int i = 1;
        System.out.println(i++);
    }
    
    public static final Singleton getInstance() {
        return Holder.singleton;
    }
    
    public static void main(String[] args) {
        
        for (int i = 0; i < 10; i++) {
            Singleton.getInstance();    
        }
    }
}

你可能感兴趣的:(java)