mardi 22 novembre 2016

How to use Java reflection?

Currently I am using instanceof, but disadvantage of that I have to add plenty of if statements to check the type of instances:

   List<Object> objectList = fetchObjectList();
   for(Object obj: ObjectList){
      if(obj instanceof Student){
        Student student = (Student)obj;
        }
       if(obj instanceof Teachers){
        Teachers teachers= (Teachers)obj;

        }

      }

Rather than this I'd like to use the java refection API. How can I achieve this with the help of reflection?





Aucun commentaire:

Enregistrer un commentaire