Это видео недоступно.
Сожалеем об этом.

How to prevent singleton class from Reflection | serialization | Cloning | Java Techie

Поделиться
HTML-код
  • Опубликовано: 25 ноя 2018
  • This video will explain you what are the various way we can break singleton behavior and how we can prevent them using java
    #JavaTechie #Singleton #Reflection #Serialization #clone
    GitHub:
    github.com/Jav...
    Blogs:
    javagyanmantra...
    Facebook:
    / 919464521471923
    Like and subscribe

Комментарии • 73

  • @parthdalvi5855
    @parthdalvi5855 6 месяцев назад +1

    Thank you for sharing this. Its really easy to understand.

  • @iltwrrm
    @iltwrrm 3 месяца назад

    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

  • @saumitrasaxena8470
    @saumitrasaxena8470 Год назад +2

    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.

  • @swathiallu9819
    @swathiallu9819 4 года назад +1

    Very good explanation. Its clean and clear. Thanks

  • @balajibollu7197
    @balajibollu7197 3 месяца назад +1

    super thanks bro sharing for knowledge

  • @jibandeepbal1639
    @jibandeepbal1639 3 года назад +1

    Very well explained. Thank you for sharing your knowledge.

  • @dineshkumar-lf2vk
    @dineshkumar-lf2vk 3 года назад +1

    bhai maza hi tumari videos ko dekhne me aata h. Hindi

  • @sushilrajiwade3551
    @sushilrajiwade3551 3 года назад +1

    Very Nice Explanation!!! Thank You

  • @myakhandbharat-2024
    @myakhandbharat-2024 Год назад +1

    Very good Information Man 🙂.

  • @AmitGadaley17
    @AmitGadaley17 3 года назад +1

    Very useful and well explained!!

  • @tulasiramsunkara
    @tulasiramsunkara 2 года назад +1

    Excellent explanation

  • @kumarinidhi90
    @kumarinidhi90 Год назад

    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 ..

  • @RAVI_GIT
    @RAVI_GIT 2 года назад +1

    Excellent Idea

  • @bhaktilatadash7619
    @bhaktilatadash7619 3 года назад +1

    Nice explanation..Thank u

  • @nagarajanerode
    @nagarajanerode 3 года назад +1

    Good explanations

  • @sameerpatere2983
    @sameerpatere2983 5 лет назад +1

    Thank you, sir. Nice explanation

  • @ramanareddysagili3626
    @ramanareddysagili3626 Год назад +1

    Outstanding man

  • @SanjeevKulkarniWorld
    @SanjeevKulkarniWorld 4 года назад +1

    Good work 👍

  • @sumannagella5034
    @sumannagella5034 5 лет назад +2

    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

  • @kishordige4654
    @kishordige4654 Год назад +1

    Thanks Man

  • @858Bikash
    @858Bikash 2 года назад +1

    Thank you sir🙏🙏

  • @maheshguptatallada6015
    @maheshguptatallada6015 Год назад

    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 ?

  • @maan9011
    @maan9011 5 лет назад +1

    Superb

  • @manjosh1990
    @manjosh1990 4 года назад +2

    Can you also show how to break it using classloaders and how to avoid it.

  • @abhrajitnandy
    @abhrajitnandy 11 месяцев назад +1

    For reflection case: can we return the same instance instead of throwing exception? Like how in readResolve()

    • @Javatechie
      @Javatechie  11 месяцев назад

      But how can we return from the constructor buddy

    • @abhrajitnandy
      @abhrajitnandy 11 месяцев назад +1

      @@Javatechie
      Oh ! My bad.

    • @Javatechie
      @Javatechie  11 месяцев назад

      @abhrajitnandy no problem

  • @muttaiahv2328
    @muttaiahv2328 2 года назад +1

    could you pease do the video for all other design patterns sir that would help us to understand the importance of various design patterns .

  • @preetird8385
    @preetird8385 4 года назад

    Good explanation. Thank you 👍

  • @saumitrasaxena8470
    @saumitrasaxena8470 Год назад

    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 ?

  • @greentorainchannel386
    @greentorainchannel386 2 года назад +1

    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?

    • @Javatechie
      @Javatechie  2 года назад +1

      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

  • @palaniappanrm6277
    @palaniappanrm6277 4 года назад +1

    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?

    • @Javatechie
      @Javatechie  4 года назад +1

      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

    • @palaniappanrm6277
      @palaniappanrm6277 4 года назад +1

      @@Javatechie I was pointing to lazy load approach only. Thanks.

  • @kishorekumar2554
    @kishorekumar2554 2 года назад +1

    Thank you

  • @priyankawagh5217
    @priyankawagh5217 4 года назад +2

    Perfect!

  • @mohammadmujahid8850
    @mohammadmujahid8850 4 года назад +1

    ultimate.

  • @cvarun9221
    @cvarun9221 3 года назад +1

    WATCH @ 1.5x speed, save time. Thanks !

  • @chetankhandave1072
    @chetankhandave1072 Год назад

    Is it better to make static variable as volatile ?

  • @anirudhjadhav9663
    @anirudhjadhav9663 3 года назад +1

    But bro! If you create an object by using the reflection first and second the getInstance then you will end with two instances.

    • @Javatechie
      @Javatechie  3 года назад

      That's the reason constructor level itself I added null check

    • @anirudhjadhav9663
      @anirudhjadhav9663 3 года назад

      @@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());

    • @sumansatpathy2862
      @sumansatpathy2862 2 года назад

      @@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;

      }

  • @sarikagarg8185
    @sarikagarg8185 3 года назад +1

    Can we use transient keyword at declaration place of instance. So that object creation due to serialisation could be avoided

    • @Javatechie
      @Javatechie  3 года назад

      Serialization never create object . Object can be created using deserialization

    • @sarikagarg8185
      @sarikagarg8185 3 года назад +1

      @@Javatechie but if objects are not mapped in file then how deserialization will do anything ?

    • @Javatechie
      @Javatechie  3 года назад

      Can we perform deserialization from only file ?

    • @sarikagarg8185
      @sarikagarg8185 3 года назад +1

      @@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

    • @Javatechie
      @Javatechie  3 года назад

      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 ?

  • @rabindrakumar949
    @rabindrakumar949 4 года назад +1

    Without throwing exception we should return the same object. With this the applications won't break because of exception.

  • @minatimahapatra4182
    @minatimahapatra4182 5 лет назад

    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.

    • @Javatechie
      @Javatechie  5 лет назад

      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

  • @satishkumar1799
    @satishkumar1799 5 лет назад +1

    How can we avoid breaking singleton in case of multiple jvms... May be load balancing case...

    • @Javatechie
      @Javatechie  5 лет назад +1

      Singleton can't be safe in multiple JVM .
      Singleton : one instance per JVM

    • @satishkumar1799
      @satishkumar1799 5 лет назад +1

      @@Javatechie Thanks for ur response... can u plzz upload end to end spring rest service based app which maintenance user session using basic authentication..

    • @Javatechie
      @Javatechie  5 лет назад +1

      @@satishkumar1799 yeah sure will upload

    • @satishkumar1799
      @satishkumar1799 5 лет назад +1

      @@Javatechie Thank You!

  • @saurabhkailashchandrapuran8609
    @saurabhkailashchandrapuran8609 3 года назад +1

    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.

  • @saurabhpatil2156
    @saurabhpatil2156 Год назад

    I can still break your singleton code by executing reflection first and calling getInstance() later. I will have 2 objects of your class

  • @AmritAgarwal07
    @AmritAgarwal07 Год назад

    1 more way is interfaces and enum also singletons[i].reference = new Singleton();