13. Java Singleton and Immutable Class Explained with Examples | Java Classes in Depth - Part4
Вставка
- Опубліковано 8 чер 2024
- ➡️ Notes Link: Shared in the Member Community Post (If you are Member of this channel, then pls check the Member community post, i have shared the Notes link there)
➡️ Join this channel to get access to member only perks:
/ @conceptandcoding
Topics which i have covered in this video:
- Singleton Class
- Eager Initialisation
- Lazy Initialisation
- Synchronised Method
- Double Checked Locking
- Bill Pugh Singleton Solution
- Enum Singleton Solution
- Immutable Class
- Wrapper Class
(Wrapper class covered in : • 6. Java Variables - Pa... )
Chapters:
00:00 - Introduction
00:19 - Singleton Class Definition
02:00 - Eager Initialisation (Singleton)
05:01 - Lazy Initialisation (Singleton)
06:28 - Synchronised Method (Singleton)
08:37 - Double-Checked Locking (Singleton)
16:14 - Bill Pugh (Singleton) Solution
18:35 - Enum(Singleton) Solution
19:52 - Immutable Class
27:21 - Wrapper Class
#java #softwareengineer #inteview
Hi Shrayansh, I have been following this Java series from sometime and just wanted to shout out that you have made this learning enjoyable for us. You really teach things from first principle which is just amazing. Keep up the amazing work.
By declaring the inner class as static, it's only loaded into memory when it's referenced for the first time. This means that the instance of the Singleton class is created lazily, i.e., only when the getInstance() method is called for the first time. This helps improve performance by avoiding unnecessary early instantiation.
right
awesome video
Great Video Shrayansh. Just a small question for the Immutable class section. Should we not do a deep copy in the parameterized constructor as well, just like the getter method for collection classes? Because the original object can be modified if someone makes an addition to the petNameList right?
If you are making a playlist, make them visible to each users otherwise there is no need ir you can sell your courses. I started following your channel and subscribed it too but here scene is different. One video available and the next is not( available for members with higher price) .. leaving it . Thanks
java is always available to members only from start. Few videos made open based on request as many engineers need to watch few videos before taking membership.
And i dont think 159Rs is higher for complete in depth java playlist.
But thats my view, but wish you good luck for your future endeavours.
Hello..thank @Shryansh for this educative and awesome content.. I have a question, please anyone who understands the question and has a clue to the answer can still please help me.
The Question is:
According to the Singleton session were different types of Singleton class was touched. If the Synchronized Singleton class is solving the two thread issue, which Lazy initialization is facing...Then how Eager initialization managing the two thread issue Synchronize Singleton class is solving.
We can create a new arraylist in the constructor and make a copy of the list and that would solve the problem too ., right?
In the constructor we should do new List(petNamelist);
Please help in understanding volatile keyword. My though is all operation that jvm performs, heaps, stack are all inmemory(cache). All our operations are having inMemory. that how disk/persistent memory came in picture. Is there any disk memory associate with jvm?
Check this video: see if this help in understanding Volatile keyword and use.
ua-cam.com/video/upfrQvOgC24/v-deo.html
can you please cover Multithreading and Concurrency in-depth, Sir?would be big help
its in my bucket list
Learn Brian Goetz book. It is awesome.
hi Sj, can you please cover a session that explain Strings in details?
well 2:38 how can you create object just inside class, it should atleast be inside any method know? like main or something
you mentioned we have discussed about string pool, in which video? i couldn't find
check this : ua-cam.com/video/Q6_jrvz-R7w/v-deo.html
with CompleteableFuture and async calls and Listenable ..with such topics
sure, will cover those.
when we do eager initialisation, there wont be an issue of creating multiple instances of the same class right even if multiple threads are trying to get the object?
Yes bcoz it's static, it's a class object
We can do it by Reflections
Can you write subclass for singleton class
At 17:43 as INSTANCE_OBJECT is private then how can outside class access it as private variable can only be used within the same class.
we are using public get and set method to access the private variables
Hello Shrayansh, Thank you so much for such great explanation.
One doubt - in lazy initialization as we have already made the variable to static then how come multiple threads working parallelly create 2 copy of it.. As it is class variable now. I mean to ask why making the constructor private & making the variable to static only does not help??
Making the variable static only ensures its shared across instances, but without lazy initialization control, multiple threads can create multiple instances before the variable is initialized, leading to duplicate instances. Making the constructor private prevents direct instantiation, ensuring lazy initialization is properly enforced.
I went through the community posts but couldnt find the notes of any class. Please tell where to find the notes ???????????
pls check Member community post. there you will find my post where i shared the notes (some are missing too)
in immutable class it should be
this,petNameList = Collections.unmodifiableList(new ArrayList(petNameList));
please correct if I am wrong
Right
Great Video , Shreyansh ! If I want to join this channel wit Membership perks, will this be recurring payment every month??
yea but you can compete it within a month or download it too
bhaiya, please increase the frequency of the vedio .. upto when we start springboot??
..kindly respond
10 videos max Java videos left. After that I will start springboot.
Generally every weekend i upload the Java video, this weekend I got some personal work. But i will upload Java video soon.
in an immutable class the member variables need not be final in this implementation i guess, am i correct? coz we cant change the values anyways as there are no setters and also they are not directly accessable
Yes, since variables are private and we don't have setters then we don't need to mark them as final.
@@thespptechs2851 thnx man
hi right, its not required, but say its a good practice (just add another layer of protection to the fields)
@@ConceptandCoding hmm okay thanks man
Bhaiji Lazy initialization me multiple thread bhi aa gaye same time par tab bhi 2 object kaise banenge?? conObject toh static hai na
2 threads will try to create object with "new" keyword, memory mein to 2 objects ban jayenge.
But bcoz of static in code it will keep only 1 reference of the 2 objects created.
@@ConceptandCoding So basically it will create 2 objects but both will reference to one only? Other object will be cleared by Garbage collector? If yes then anyway it solves our problem right?
@@harshalgarg9656 no that's defeat the purpose of Singleton class.
2 objects should not be created at any point of time.
Couple of points:
- If it's a very expensive to create object, it will be done 2 times.
- GC will not run instantly. So it will be in memory might be for some time.
@ConceptandCoding Each thread have their own stack memory and if we make object non-static then each individual thread will have its own unique instance variable.
On the other hand, static members are the part of metaSpace (within heap memory) , so all thread can access only one static instance variable.
If there is any inaccuracies in my understanding, then please guide me. Thank you sir! your tutorials have been immensely beneficial.
what is the problem with eager and lazy intilaization can. u explain me clearly??
Pls check this ua-cam.com/video/upfrQvOgC24/v-deo.html
Sir till when java playlist be completed?
By next month most probably
How Bill pugs implementation is thread safe ???
because it relies on the classloader to initialize the Singleton instance, ensuring that it is created only once in a thread-safe manner.
@@ConceptandCoding class loading in Java is inherently thread-safe.
Can you please explain this statement???
What all do I get from your paid training sources?
With "Unlock LLD, HLD and Java" level, many interview questions with solutions and many Java videos will be opened up.
In the case of double locking can you please explain what will be the case when thread1 preempts after checking null inside synchronised block, its an imp point which i was hoping should have been mentioned in video but its not, it would have been better if things are really explained in a better way and in depth rather than just covering a bunch of topics otherwise it will be of no help in interviews, interviewer asked me this imp point which you missed entirely
kindly check this
28. BUG in Double-Checked Locking of Singleton Pattern & its Fix
ua-cam.com/video/upfrQvOgC24/v-deo.html