mardi 18 septembre 2018

Using Java reflection class to create an object and use it in arraylist

I started to use Java Reflection recently but currently stuck at this.

So I have something like this:

Class<?> dogClass = Class.forName("com.example.dog");
Object dogObject = dogClass.newInstance();

I would like to use the above object in this arraylist:

List<Dog> dogList = new ArrayList();

So in normal case:

for(Dog d : dogList) {
....
.... 
}

But when I tried to use the java reflection, it doesn't work..

for(dogObject d : dogList) {
....
.... 
}

Can anyone enlighten me please? Thank you.





Aucun commentaire:

Enregistrer un commentaire