Is there any benefit to have a private static class implement A?
Well, it hides the implementation away completely, so from an encapsulation point of view it's quite nice. One situation I've seen this in a few times is custom comparators. For example:
public class Person
{
public static final Comparator<Person> NAME_COMPARATOR = new NameComparator();
public static final Comparator<Person> AGE_COMPARATOR = new AgeComparator();
// Name, age etc properties
private static class NameComparator implements Comparator<Person>
{
...
}
private static class AgeComparator implements Comparator<Person>
{
...
}
}
There's no real need for the comparator implementation classes to be visible outside Person
, and it's nice to be able to get an instance easily via the public static field.
No callers need to know the implementation - there could just be one comparator class which takes parameters, for example - they just express which comparator they want via the constants. (You could equally use an enum for this, of course.)
NameComparator
andAgeComparator
as non-public classes? I'm not sure, but doesn't adding a static inner class make it binary incompatible? – Midnight