Singleton class-Violation

private static MyObject myObject;
private MyObject (){
}
public static synchronized MyObject getInstanceObject(){
if(myObject==null){

myObject=new Object();
}

return myObject
}

It could happen that the access method may be called twice from 2 different classes at the same time and hence more than one object being created. This could violate the design patter principle. In order to prevent the simultaneous invocation of the getter method by 2 threads or classes simultaneously we add the synchronized keyword to the method declaration

Comments

Popular posts from this blog

Spring MVC- Redirect from one controller to another

Jquery Table Inline Edititing