public class SingletonObject
{
private SingletonObject()
{
// no code req'd
}
public static synchronized
SingletonObject getSingletonObject()
{
if (ref == null)
// it's ok, we can call this constructor
ref = new SingletonObject();
return ref;
}
public Object clone()
throws CloneNotSupportedException
{
throw new CloneNotSupportedException();
// that'll teach 'em
}
private static SingletonObject ref;
}
public class Clone
{
public static void main(String args[])
throws Exception
{
// Get a singleton
SingletonObject obj =
SingletonObject.
getSingletonObject();
SingletonObject clone =
(SingletonObject) obj.clone();
}
}
{
private SingletonObject()
{
// no code req'd
}
public static synchronized
SingletonObject getSingletonObject()
{
if (ref == null)
// it's ok, we can call this constructor
ref = new SingletonObject();
return ref;
}
public Object clone()
throws CloneNotSupportedException
{
throw new CloneNotSupportedException();
// that'll teach 'em
}
private static SingletonObject ref;
}
public class Clone
{
public static void main(String args[])
throws Exception
{
// Get a singleton
SingletonObject obj =
SingletonObject.
SingletonObject clone =
(SingletonObject) obj.clone();
}
}
No comments:
Post a Comment