 Hi guys welcome so today we are going to share one of the important concept of Java you can say one of the important interview questions that is asked in every Java interview what is singleton design pattern. So basically singleton pattern you can answer it this way like singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the JVM. So and remember singleton in Java and singleton in spring are there is a bit difference and it is better to know about that as a so in Java it is one instance like one instance of a class per JVM it is present and in spring it is a bit different and whenever we are explaining that thing I talk about that thing as a so how can we so after the after giving the theoretical what I can say description of singleton design pattern the next question obviously would be how to create a singleton class in Java. So we can create singleton class in Java by having a private constructor a private static variable which holds the instance variable and a public static default like get instance method which will create the object and return the object. So let us see how can we do that so basically singleton can be created like this as well right. So why do we need construct stars and all those things directly have a private sorry private static singleton class and this is an instance so we can have this have a private constructor singleton class constructor so private constructor have a public method this method is a static method again so public static and can we do this directly return instance and directly here I can create the object so I with this thing we can even say that only one thread can act no matter how many threads act only one instance will be created as because this code gets executed when the class is getting loaded itself so what the issue here is even if nobody wants the object of the singleton class even then in that scenario the object is getting created so that is not the expected way the code should work only if at all someone is requesting at that time only our object should get created this is also known as eager what I can say eager instantiation or eager object creation we can say and there is one more way as well we can have a static block here and same logic can be written inside the static block and we can return the instance directly but again there is also the again the issue is the static block gets executed at the time of class loading itself and again an eager initialization will happen so that is not what we want so what we want only whenever someone is requesting at that time only we need our object right so what we can do here directly we can have one if condition check the instance if it is instantiated already or not if it is not then call the class click all the constructor and return this that's it we are done a singleton instance will be created here and it will be returned but the issue here is the implementation that definitely this implementation works perfectly fine when when it is only single threaded environment but when it comes to multi-threaded environments it can cause reuse of reuse if multiple threads are inside that if condition multiple threads are here that they have passed this check then there can be multiple what I can say instances getting created of the singleton class and the singleton pattern will be get destroyed here so how to now we have to make this thread safe how can we make thread safe it is very simple so we can directly have one synchronized keyword we can sorry we can create a synchronized block synchronized block and our issue is resolved the multi-threading environment also the only one thread will be acting upon this method and we the singleton pattern doesn't get destroyed but here the issue is with performance the performance get degraded even though it is for the initial one two threads but still they will log the entire object and it will create the performance so to overcome this one we can go with double checked locking so what is double checked locking I'll explain remove the synchronized keyword here here in this thing we can have two if checks how so we have one synchronized if so firstly we'll check then we will have one synchronized synchronized keyword synchronized block can have one synchronized block synchronized all this class I hope everyone knows how to create a synchronized block so we have synchronized block then again inside the synchronized block have one more if condition this if condition if inside this then we can create our object creation can happen here so now what will happen our issue got resolved so the performance that used to have impact when we are having the synchronized method now that gets eradicated and this way and this is a perfect way to create a singleton class I once it's perfect way but this is one of the best way best best approaches to follow singleton design pattern also how to create a singleton class the best possible way in the next section I will explain you how can we break this singleton pattern here just wait for the next section so guys we have created the singleton class now let us see how can we break the singleton prototype singleton functionality I've created one class let me quickly copy the see there are a number of ways to break the functional singleton functionality I am using reflection here to break it so the very instance I will get one instance of singleton class copy paste S2 make that so I am creating calling singleton class again let's see how this S1 S1 and S2 let us have the hash code and let's run this now see if you notice both have written printed the same hash code that means only one instance variable has been created now let us see how can we break so to break it see what I do class dot class dot get declared constructors this will return the declared constructors so constructors equals let us create a local variable this is of type singleton class type cast is here as well so we got the constructors now I will iterate through each constructor let us create a loop variable constructor we got dot set accessible to true now see the magic now I am reassigning the S2 to constructor dot new instance okay and now I will print this S2 S2 dot hash code through exception okay now see what will happen see the magic boom the new hash code has been printed with first that means there are two instance variables now now how can we resolve this how can we solve this so one of the easiest and the quickest way is I have just have one if condition here but this is debatable or we can do in any way we can have an enum here or we can do this if this is not one then directly through an exception new add rows so let me create one message here not allowed wherever we are using the constructor at all those spaces we need to throw our hand in this directly through now run the same code and see what will happen when we got to one instance the image for getting printed now when we try to create a new instance directly using reflection you got the exception saying not allowed this is one of the way we can do and this is as I said this is debatable we can directly have an enum here and we can solve this issue and the other ways using how singleton functionality can be break of these classes are using serializable when we serialize and when we deserialize it the serial functionality will be breaking here so to handle that when serialization and deserialization we need to override one method called read resolve also if you are overriding the read resolve on calling that get instance method then the issue will be resolved and in the clone using cloning also we can break this functionality and in that scenario whenever the clone method is called we need to over at the method and we need to throw an exception from the clone method saying that our clone is not alone and we can resolve that in that way and that's it in this video i hope you liked it just one thing i have explained it you want the other ways how to break and make singleton or if you want that scenarios as well you can comment comment it out in the comment section i'll create the video on that as well so let's keep it short i have shown you one example there are few other examples as well and thank you for watching