Это видео недоступно.
Сожалеем об этом.
13. Java Singleton and Immutable Class Explained with Examples | Java Classes in Depth - Part4
HTML-код
- Опубликовано: 14 авг 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
In the constructor we should do new List(petNamelist);
We can create a new arraylist in the constructor and make a copy of the list and that would solve the problem too ., 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.
I would considering the depth of the videos,, it's heavily discounted
@@ConceptandCoding check negative comments on your videos and u will find a pattern. 😅
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?
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.
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.
In Eager Initialization, the dbconnection obj is already created so multiple threads can call the getInstance() method simultaneously , there is no synchronization maintained .
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)
well 2:38 how can you create object just inside class, it should atleast be inside any method know? like main or something
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.
ruclips.net/video/upfrQvOgC24/видео.html
Writes anywhere when performed are eventually made flushed to Disk for persistent storage , so that in future the same change remain persistent , in general we do read and write from/to caches then Inmemory where writes are periodically flushed to disk . It is something like Write Ahead logs where schedulers are set to periodically make updates to disk . Volatile does the same thing it like instructing thread to read and write from/to memory skipping cache .
awesome video
you mentioned we have discussed about string pool, in which video? i couldn't find
check this : ruclips.net/video/Q6_jrvz-R7w/видео.html
Hi i started with java palylist but there is no proper sequence followed actually, threads are at last part ,but you used the concept here , did i miss something or you made threads earlier?
In singleton enum approach , where are we intializing INSTANCE variable ?
They are initlised by default constructor
with CompleteableFuture and async calls and Listenable ..with such topics
sure, will cover those.
hi Sj, can you please cover a session that explain Strings in details?
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
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
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
i to got the same doubt read this
In Java, the `private` access modifier restricts access to the members of a class only within the class itself. However, the nested static class `EagerHelper` is a member of the outer class `Eager`. This means that the outer class `Eager` can access the `private` members of its nested class `EagerHelper`.
Here's how it works step-by-step:
1. **Nested Class**: The `EagerHelper` class is a static nested class inside the `Eager` class.
2. **Private Static Final Field**: The `EagerHelper` class has a private static final field `obj` that is initialized with a new instance of the `Eager` class.
3. **Access from Outer Class**: The outer class `Eager` can access the `obj` field of its nested class `EagerHelper` because it's a member of the `Eager` class.
So, even though the `obj` field in `EagerHelper` is `private`, it can still be accessed from the `Eager` class. This is a feature of Java's access control for nested classes. Here’s the explanation with the code:
```java
public class Eager {
public static Eager Eagerobj;
private Eager() {
// private constructor to prevent instantiation
}
// Static nested class
private static class EagerHelper {
// Private static final field
private static final Eager obj = new Eager();
}
// Public static method to get the singleton instance
public static Eager getEager() {
// Accessing the private field of the nested class
return EagerHelper.obj;
}
}
public class Main {
public static void main(String[] args) {
Eager a = Eager.getEager();
// You can add additional logic here to verify the singleton behavior if needed
}
}
```
In summary, the `Eager` class can access the `private` field `obj` of the nested static class `EagerHelper` because it’s within the scope of the `Eager` class, which is allowed by Java's access control rules for nested classes.
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.
Can you write subclass for singleton class
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???
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
what is the problem with eager and lazy intilaization can. u explain me clearly??
Pls check this ruclips.net/video/upfrQvOgC24/видео.html
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.
Sir till when java playlist be completed?
By next month most probably
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
ruclips.net/video/upfrQvOgC24/видео.html