How to use getInstance method in Java | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
- 1

How to use getInstance method in Java

27th Feb 2020, 12:45 PM
B Poojitha
B Poojitha - avatar
4 Antworten
+ 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