You are just amazing as usual, thanks for crisps explanation.😊
@saumitrasaxena84702 жыл бұрын
Hi , First of all congrats for good explanation . I did more deeper analysis into it and can see this code can also break. Let's see how. Suppose we create 2 Threads . Thread-1 calls getInstance() and reaches line 21 , and now 2nd Thread I used reflection code and new Object is created via constructor as instance is still null. And now again when line 21 is executed ,new instance is created again.So two object is created.
@iltwrrm6 ай бұрын
thanks Java Techie. I love your videos and how you enhance our understanding about concepts such as this, creating a Singleton, and then breaking it; really helps our understanding and furthers our comprehension and knowledge. love it. keep ‘em coming
@parthdalvi58558 ай бұрын
Thank you for sharing this. Its really easy to understand.
@dineshkumar-lf2vk3 жыл бұрын
bhai maza hi tumari videos ko dekhne me aata h. Hindi
@jibandeepbal16393 жыл бұрын
Very well explained. Thank you for sharing your knowledge.
@myakhandbharat-2024 Жыл бұрын
Very good Information Man 🙂.
@kumarinidhi902 жыл бұрын
wonderful and knowledgeable video.. Really understood the concepts and it is very useful video. I have a request, please make a similar series for JavaScript interview questions and a similar series for react js interview questions ..
@balajibollu71976 ай бұрын
super thanks bro sharing for knowledge
@swathiallu98194 жыл бұрын
Very good explanation. Its clean and clear. Thanks
@RAVI_GIT2 жыл бұрын
Excellent Idea
@ramanareddysagili36262 жыл бұрын
Outstanding man
@sushilrajiwade35513 жыл бұрын
Very Nice Explanation!!! Thank You
@tulasiramsunkara3 жыл бұрын
Excellent explanation
@sumannagella50345 жыл бұрын
hi sir thanks for posting very useful videos, please post some other most important concepts of real time and also some more important design patterns. so that it will be helpful for lot of developers
@Javatechie5 жыл бұрын
Ok will do it suman
@nagarajanerode3 жыл бұрын
Good explanations
@cvarun92213 жыл бұрын
WATCH @ 1.5x speed, save time. Thanks !
@AmitGadaley173 жыл бұрын
Very useful and well explained!!
@abhrajitnandy Жыл бұрын
For reflection case: can we return the same instance instead of throwing exception? Like how in readResolve()
@Javatechie Жыл бұрын
But how can we return from the constructor buddy
@abhrajitnandy Жыл бұрын
@@Javatechie Oh ! My bad.
@Javatechie Жыл бұрын
@abhrajitnandy no problem
@bhaktilatadash76193 жыл бұрын
Nice explanation..Thank u
@maheshguptatallada60152 жыл бұрын
Hi Sir, @Java Techie Could you please clarify the below. when IllegalArgumentException is runTime one, why the compiler is asking us to throw it in compile time in this code ?
@858Bikash2 жыл бұрын
Thank you sir🙏🙏
@greentorainchannel3862 жыл бұрын
Hello Sir, Thank you for detailed info on Singleton pattern. I have a question Sir, don't we need to add null check in readResolve() method? Could you please help in clarifying this Sir?
@Javatechie2 жыл бұрын
No readResolve only called at the time of deserialize so here we want to return the existing instance . that's why no null check required here
@SanjeevKulkarniWorld4 жыл бұрын
Good work 👍
@muttaiahv23282 жыл бұрын
could you pease do the video for all other design patterns sir that would help us to understand the importance of various design patterns .
@Javatechie2 жыл бұрын
Will do that
@sameerpatere29835 жыл бұрын
Thank you, sir. Nice explanation
@manjosh19905 жыл бұрын
Can you also show how to break it using classloaders and how to avoid it.
@palaniappanrm62775 жыл бұрын
Hi, let's say we export these classes as a jar to other clients. They didn't create any Singleton objects yet. They have a file that has our singleton object serialized. When they read the file and try to deserialize it, this readResolve method will get called and will return instance which is pointing to null. I think we should have a null check in the readResolve method. Right?
@Javatechie5 жыл бұрын
You are correct in lazy Singleton approach we should add null check but if it is eager Singleton approach the Then it's not required . Because on class level we are creating instance in eager loading Singleton approach
@palaniappanrm62775 жыл бұрын
@@Javatechie I was pointing to lazy load approach only. Thanks.
@saumitrasaxena84702 жыл бұрын
if I create an object using reflection first -- I will have one object as first-time instance is null so object is created using reflection and 2nd time i call getInstance then I will get object created by reflection ....Am i correct ?
@maan90115 жыл бұрын
Superb
@chetankhandave1072 Жыл бұрын
Is it better to make static variable as volatile ?
@kishorekumar25542 жыл бұрын
Thank you
@preetird83854 жыл бұрын
Good explanation. Thank you 👍
@kishordige46542 жыл бұрын
Thanks Man
@sarikagarg81853 жыл бұрын
Can we use transient keyword at declaration place of instance. So that object creation due to serialisation could be avoided
@Javatechie3 жыл бұрын
Serialization never create object . Object can be created using deserialization
@sarikagarg81853 жыл бұрын
@@Javatechie but if objects are not mapped in file then how deserialization will do anything ?
@Javatechie3 жыл бұрын
Can we perform deserialization from only file ?
@sarikagarg81853 жыл бұрын
@@Javatechie As per the code you shown a serialization is done in a file. I also have a query that once a class is created which don't extends to myclone and don't implements serelizable interface the how some othe program would be able to make clone out of that class
@Javatechie3 жыл бұрын
Let's say your singleton class extends from some other class and that class extends from Serializable then in that situation anyone can deserilize your singleton class right ?
@priyankawagh52174 жыл бұрын
Perfect!
@mohammadmujahid88505 жыл бұрын
ultimate.
@minatimahapatra41825 жыл бұрын
hi Sir. I have tried with implementing Cloneable(I) in LazySingleton class and worked fine with breaking Singleton behaviour. So why should we go for MYClone class.
@Javatechie5 жыл бұрын
In some worst senario we may need my class to be extends from some other class .so In such case if that class implement Clonable then it will break the rules . So for demonstrate purpose I created this MyClone class
@saurabhkailashchandrapuran86093 жыл бұрын
Basant sir plz make more videos on interview question like circular dependency on beans and many more which you think will be help in interview.
@Javatechie3 жыл бұрын
Okay I will
@rabindrakumar9495 жыл бұрын
Without throwing exception we should return the same object. With this the applications won't break because of exception.
@Javatechie5 жыл бұрын
Yes we can
@satishkumar17995 жыл бұрын
How can we avoid breaking singleton in case of multiple jvms... May be load balancing case...
@Javatechie5 жыл бұрын
Singleton can't be safe in multiple JVM . Singleton : one instance per JVM
@satishkumar17995 жыл бұрын
@@Javatechie Thanks for ur response... can u plzz upload end to end spring rest service based app which maintenance user session using basic authentication..
@Javatechie5 жыл бұрын
@@satishkumar1799 yeah sure will upload
@satishkumar17995 жыл бұрын
@@Javatechie Thank You!
@anirudhjadhav96633 жыл бұрын
But bro! If you create an object by using the reflection first and second the getInstance then you will end with two instances.
@Javatechie3 жыл бұрын
That's the reason constructor level itself I added null check
@anirudhjadhav96633 жыл бұрын
@@Javatechie Can you try this code:- LazySingleton reflectionInstance = null; Class clazz = Class.forName("LazySingleton "); Constructor s3Constructor = clazz.getDeclaredConstructor(); s3Constructor.setAccessible(true); reflectionInstance = s3Constructor.newInstance(); System.out.println("Reflection hashcode :- " + reflectionInstance .hashCode()); LazySingleton instance1 = LazySingleton.getInstance(); System.out.println("Instance hashcode :- " + instance1.hashCode());
@sumansatpathy28622 жыл бұрын
@@anirudhjadhav9663 Just saw your comments & I had also same doubt. Tried to solve. Could you please check this below sol if that works. Please let me know if you find any catch here. private static boolean isCallFromMyGetInstance = false; private static MySingletonClass obj; private MySingletonClass(){ if(obj!=null || isCallFromMyGetInstance==false) { throw new IllegalStateException("Object can't be created using reflection for this class"); } } public static MySingletonClass getInstance() { if(obj==null) { synchronized (MySingletonClass.class) { if(obj==null) isCallFromMyGetInstance=true; obj=new MySingletonClass(); } } return obj; }
@AmritAgarwal072 жыл бұрын
1 more way is interfaces and enum also singletons[i].reference = new Singleton();
@saurabhpatil2156 Жыл бұрын
I can still break your singleton code by executing reflection first and calling getInstance() later. I will have 2 objects of your class