What is the difference between singleton class and a normal class | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula gr√°tis
+ 1

What is the difference between singleton class and a normal class

please give an example

29th Jul 2017, 8:58 AM
Devbrath
Devbrath - avatar
3 Respostas
+ 3
A singleton class can only have one instance whereas a normal class can have many. Ie. A normal class allows you do this: NormalClass object1 = new NormalClass() NormalClass object2 = new NormalClass() For a singleton class you must do SingletonClass.getInstance() The get instance checks whether the object has been created and creates the object in the same way that a normal class is instantiated but this is only done once. If it has already been accessed before, it will return you the object.
29th Jul 2017, 9:10 AM
Hassie
Hassie - avatar
+ 2
so it means if I try to create another object of singleton class then it will show error
29th Jul 2017, 9:20 AM
Devbrath
Devbrath - avatar
+ 2
Yes
29th Jul 2017, 2:30 PM
Hassie
Hassie - avatar