I need to create and return a generic list of static nested classes objects.
For example :
if there is a class Outer containing static nested classes as InnerOne/Two/Three and so on.. with their own members and methods.
and
I am trying to create and retrieve their instance through Factory class.
How can I do it ?
How should I declare and define my "genericList" as shown in below code.
so that later I can "typecast" the list objects to their respective class object by making use of "instanceof" operator and call upon their respective "overloaded" functions ??
public class Outer{
public static class InnerOne{
//members and methods
};
public static class InnerTwo{
//members and methods
};
public static class InnerThree{
//members and methods
};
}
public class factory{
public static List<?> getInstanceList(List<String> instancetypes){
List<?> genericList = new ArrayList<>();
for(String instanceType : instancetypes){
if(instanceType.equals("InnerOne")){
genericList.add(new InnerOne());
}
else if(instanceType.equals("InnerTwo")){
genericList.add(new InnerTwo());
}else{
genericList.add(new InnerThree());
}
}
return genericList;
}
}