- 1

How to use getInstance method in Java

27th Feb 2020, 12:45 PM
B Poojitha
B Poojitha - avatar
4 ответов
+ 4
are you talking about Instanceof
27th Feb 2020, 1:52 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 3
what do you mean by getInstance
27th Feb 2020, 12:50 PM
Sumit Programmer😎😎
Sumit Programmer😎😎 - avatar
+ 2
in singleton pattern ? it goes like this public SingletonClass{ private static SingletonClass instance; private SingletonClass(){} public static getInstance(){ if(!instance == null) instance = new SingletonClass(); return instance; } } to call simply SingletonClass sc = SingletonClass.getInstance(); so why getInstance instead of new ? this pattern limit a class to only have limited amount of object, you can see the constructor are private so no new object be created. except all the object defined within its own class, in this case is instance.
27th Feb 2020, 1:52 PM
Taste
Taste - avatar
0
If you are a beginner than I don't understand how did you even come across this term. It is not generally used or may be I have not come across this one. But you can read about it here. https://www.geeksforgeeks.org/singleton-class-java/ https://www.geeksforgeeks.org/java-signature-getinstance-method-with-examples/
27th Feb 2020, 1:53 PM
Avinesh
Avinesh - avatar