-1

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.

BATMAN_2008
  • 2,788
  • 3
  • 31
  • 98
  • @maloomeister Thanks for your response. Basically I want to access the `locationFinder` method from `MyClass` by passing the values. Do you mean `LocationBasedFormatter result = loc.Location("44.54","94.54");`? – BATMAN_2008 Jan 18 '21 at 10:49
  • If you need to access `locationFinder` method, maybe you should have declared it in interface `Formatter`? – Nowhere Man Jan 18 '21 at 10:59
  • @AlexRudenko Yes you are correct, I tried that, and it's working. Just out of curiosity what if I do not want to add the `Method` in `Interface`? How can I access it then. Because in another class I have methods which take different parameter as input and produce different output so I cannot add method in `Interface` in those cases how can I access method and get result without add the Method in interface – BATMAN_2008 Jan 18 '21 at 11:10

2 Answers2

1

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.

Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
  • Thanks for the response. I tried that, and it's working. Just out of curiosity what if I do not want to add the Method in Interface? How can I access it then. Because in another class I have methods which take different parameter as input and produce different output so I cannot add method in Interface in those cases how can I access method and get result without add the Method in interface – BATMAN_2008 Jan 18 '21 at 11:13
  • If you need to allow access to other methods of the private nested class than defined in the contract/interface, possibly this class should not be private and nested at all? It's up to you how you design your class and interface structure :) – Nowhere Man Jan 18 '21 at 11:19
  • Cool. Thanks for your answer and clarification. Have a great day ahead :) – BATMAN_2008 Jan 18 '21 at 11:23
0

Technically LocationBasedFormatter is not an inner class. It's a static nested class.

Add a public static getLocationBasedFormatterInstance() method in the LocationFormatter class. This getLocationBasedFormatterInstance() could return the instance of LocationBaedFomratter

public class LocationFormatter {
  ... 
  public static getLocationBasedFormatterInstance(){
     LocationBasedFormatter lbf = new LocationBasedFormatter();
     return lbf;

  }

}

Then you have to create an object of your static nested class LocationBasedFormatter from your main method.

LocationFormatter.LocationBasedFormatter locationFormatter = new LocationFormatter();
LocationBaedFomratter loationBasedFomratter = locationFormatter.getLocationBasedFormatterInstance();
Razib
  • 10,965
  • 11
  • 53
  • 80
  • Thanks for your response but I am unable to create the object based on `LocationFormatter.LocationBasedFormatter` as my `LocationBasedFormatter` is private. It throws me the error `The type LocationFormatter.LocationBasedFormatter is not visible`. Is there any other way to do it by keeping the `static nested class` private? – BATMAN_2008 Jan 18 '21 at 11:18