Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

please tell me when to use getInstance() method in java. [closed]

Tags:

When to use the getInstance() method in java and what is the meaning of getInstance(null)?

locationProvider = LocationProvider.getInstance(null);  

can anyone tell me the meaning of the above line?

like image 202
Roster Avatar asked May 07 '12 05:05

Roster


People also ask

Why do we use getInstance () method in Java?

The getInstance() method of java. security. Provider class is used to return a Signature object that implements the specified signature algorithm. This method traverses the list of registered security Providers, starting with the most preferred Provider.

Why we use getInstance method in Singleton?

Here, ClassicSingleton class employs a technique known as lazy instantiation to create the singleton; as a result, the singleton instance is not created until the getInstance() method is called for the first time. This technique ensures that singleton instances are created only when needed.

What is the difference between getInstance and new?

Difference between Class#getInstance() and new operator ? Ans. Class. getInstance doesn't call the constructor whereas if we create an object using new operator , we need to have a matching constructor or copiler should provide a default constructor.

Why is getInstance static?

for a singleton pattern, getInstance IS a static method, and uses static attrs. The whole point of static is to have things associated with the class instead of a specific object. The singleton pattern guarantees that you will have one instance of an object of that type.


1 Answers

Classes that use getInstance() methods and the like are of the singleton design pattern. Basically, there will only ever be one instance of that particular class, and you get it with getInstance().

In this case, LocationProvider will only ever have one instance, since it's device-specific. Instead of creating new instances of it, you can use the shared instance by using the getInstance() method. The singleton pattern is often used in Java when dealing with things like data managers and hardware interfaces, but it shouldn't be used for too much else, since it restricts you to a single instance.

like image 140
Alexis King Avatar answered Sep 28 '22 01:09

Alexis King