S
S
Stanislav2016-10-20 18:11:25
Java
Stanislav, 2016-10-20 18:11:25

Is this version of the singleton absolutely safe?

Can you please tell me if this version of the Singleton pattern is safe?
Can it be undermined by reflection, or in some other way?

public class MySingleton implements Cloneable{
    private static MySingleton mySingleton = new MySingleton();

    private MySingleton(){}
    
    public static MySingleton getInstance(){
        return mySingleton;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return mySingleton;
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question