I'll preface this question with the fact that I am not a professional Java programmer. I come from a strong C embedded systems background, but was self-taught Java back in college.
I am creating a simple program where there are many different data "producer" classes. Each class generates some kind of data output product.
ArrayList< Class<? extends OtherClass> > list = new ArrayList< Class<? extends OtherClass> >();
//Register producers based on some criteria for the analysis being done
list.add(Producer1.class);
list.add(Producer2.class);
//Some time later, I want to create instances of those classes and link them to a 'report'
Report report = new Report();
report.addProducer( new list.get(0)() );
Of course, the above code does not work. I am able to create an ArrayList of class types, but I am not able to instantiate them at a later time (at least I am not able to find the correct syntax to do so).
I also intended originally to do this with interfaces
, but the syntax for creating an array of classes does not seem to work like extends
in the above code sample. I tried the following, but it failed syntax checking:
ArrayList< Class<? implements OtherClass> > list = new ArrayList< Class<? implements OtherClass> >();
I searched around for a suitable answer, but not knowing the correct question/terminology to ask can make that difficult. I found this solution, but their intentions may have been different.
Of course, if this can be done in a simpler way then I am open to all suggestions.