- 1
How to use getInstance method in Java
4 ответов
+ 4
are you talking about Instanceof
+ 3
what do you mean by getInstance
+ 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.
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/