Singleton | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Singleton

can anyone explain to me what is a singleton?

10th May 2017, 6:13 PM
Edward
9 Answers
+ 15
Simplest possible example... The code cannot not be run, but you can see how a singleton can be created: https://code.sololearn.com/cfhabtq79XoJ/?ref=app
10th May 2017, 6:25 PM
Tashi N
Tashi N - avatar
+ 16
Singleton is a design pattern, that allows only one instance of a class. You can use it if you need an object to be unique (only create a new object if the class wasn't instantiated before).
10th May 2017, 6:17 PM
Tashi N
Tashi N - avatar
+ 11
You're welcome ^^
10th May 2017, 6:29 PM
Tashi N
Tashi N - avatar
+ 3
Singleton is a programming pattern that provide a unique global instance. The class is responsible to create that unique instance.
10th May 2017, 6:19 PM
⏩▶Clau◀⏪
⏩▶Clau◀⏪ - avatar
+ 2
oh ok that makes sense thanks a lot
10th May 2017, 6:28 PM
Edward
+ 2
It's a common used design pattern that restricts the instantiation of a class to one object. Singleton wikipedia: https://en.wikipedia.org/wiki/Singleton_pattern
10th May 2017, 6:31 PM
Szabó Gábor
Szabó Gábor - avatar
+ 2
some days ago I demonstrated in another thread an example implementation of a singleton https://code.sololearn.com/cQsAcUted9IO/?ref=app
10th May 2017, 8:02 PM
Volker Milbrandt
Volker Milbrandt - avatar
+ 1
do you think you can give me an example or a link to an easy to understand singleton
10th May 2017, 6:18 PM
Edward
+ 1
For any Java class if we are allowed to create only one object, then such type of class is called singleton class. Some of the examples are: Runtime BusinessDeligate ServiceLocator The main advantage and central idea of singleton classes are, If several people have same requirement, then it's not recommended to create seperate object for every requirement. We have to create only one object and we can reuse the same object as many times we want, so that performance and memory utilisation will be improved. We can create our own singleton classes by using 1. private constructor 2. private static variable 3. public factory method.
27th Sep 2020, 2:20 PM
Anand Namastemath
Anand Namastemath - avatar