java Singleton - prevent multiple creation through reflection

前端 未结 6 902
无人及你
无人及你 2021-01-30 22:52

I have a singleton like this.

public class BookingFactory {

    private final static BookingFactory instance;

    static {
        instance = new BookingFactor         


        
6条回答
  •  佛祖请我去吃肉
    2021-01-30 23:51

    I highly recommend reading What is an efficient way to implement a singleton pattern in Java? - using an enum prevents what you're describing and is the recommended way to implement a singleton in java.

提交回复
热议问题