I have the following code, I wanted to know how to access the public method:locationFinder
in the private inner class:LocationBasedFormatter
of the outer class:LocationFormatter
.
Basically I would like to access the method locationFinder
from MyClass
and get the List<String>
@Getter
@Setter
public class LocationFormatter {
public static final Formatter Location(String longitute, String latitude) {
return new LocationBasedFormatter(longitute, latitude)
}
public static interface Formatter {}
@AllArgsConstructor
private static final class LocationBasedFormatter implements Formatter {
private String longitute;
private String latitude;
public final List < String > locationFinder() {
String location = "Your Long: " + this.longitute + " Your Lat: " + this.latitude;
List < String > randomList = new ArrayList < String > ()
randomList.add(location)
return randomList;
}
}
}
Following is my other main class from where I need to pass the value and access the locationFinder
method:
public class MyClass {
public static void main(String args[]) {
LocationFormatter loc = new LocationFormatter();
LocationBasedFormatter result = loc.Location("44.54", "94.54");
}
}
When I try to find the Objects/Methods on loc
or result
then I am unable to find my Method locationFinder
. I would like to access locationFinder
method and get the List
based on the values I passed to my private variable.
I am a bit confused and unable to get the method in another class. However, if I write the main method within the same class then I am able to access it.
It seems that you need to declare method locationFinder
in interface Formatter
and use this interface (which is currently just a marker interface) instead of its specific implementation in private class:
public static interface Formatter {
List<String> locationFinder();
}
Then this method will be accessible publicly:
public class MyClass {
public static void main(String args[]) {
// no need to instantiate LocationFormatter, Location is static method
Formatter result = LocationFormatter.Location("44.54", "94.54");
List<String> locationList = result.locationFinder();
}
}
However, more descriptive names should be used for classes and methods in this code.