What would happen if we change the constructor of the singleton from private to protected? How we can prevent it breaking in that case?
Singleton:
public class SingletonObject
{
private static SingletonObject ref;
private SingletonObject () //private constructor
{
System.setSecurityManager(new SecurityManager());
}
public static synchronized SingletonObject getSingletonObject()
{
if (ref == null)
ref = new SingletonObject();
return ref;
}
public Object clone() throws CloneNotSupportedException
{
throw new CloneNotSupportedException ();
}
}
For breaking the singleton the following url contains the required info cracking singleton with other ways.