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?
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?
The following code will give you the latitude and longitude. getInstance() will give back the instance of that particular class.
Criteria myCriteria = new Criteria();
myCriteria.setCostAllowed(false);
LocationProvider myLocationProvider = LocationProvider.getInstance(myCriteria);
Location myLocation = myLocationProvider.getLocation(300);
latitude = myLocation.getQualifiedCoordinates().getLatitude();
longitude = myLocation.getQualifiedCoordinates().getLongitude();
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.
Method getInstance()
is called factory method. It is used for singleton class creation. That means only one instance of that class will be created and others will get reference of that class.
Calendar.getInstance() == Calendar.getInstance()
is false
. –
Donato The following code will give you the latitude and longitude. getInstance() will give back the instance of that particular class.
Criteria myCriteria = new Criteria();
myCriteria.setCostAllowed(false);
LocationProvider myLocationProvider = LocationProvider.getInstance(myCriteria);
Location myLocation = myLocationProvider.getLocation(300);
latitude = myLocation.getQualifiedCoordinates().getLatitude();
longitude = myLocation.getQualifiedCoordinates().getLongitude();
© 2022 - 2024 — McMap. All rights reserved.