Okay, here’s the setup:
EnclosingClass {
public interface ClassFactory {
public static SomeClass getInstance(int which);
}
private static ClassFactoryImpl {
@Override
public static SomeClass getInstance(int which) {
switch(which) {
case 1:
return new SomeClassSubclassA();
case 2:
return new SomeClassSubclassB();
...
}
}
}
}
I would like to be able to issue statements along the line of:
SomeClass x = EnclosingClass.ClassFactory.getInstance(instanceClassRequest);
Is this possible? If not, how can I access a static nested class through only the interface it implements?
The short answer is “no.” You’ll need to make an instance of your implementation class and put it in a static variable. It’ll look like this:
Also note that the method on the instance is no longer static.
Then code that invokes it would look like this: