vendredi 7 août 2015

Can we cast a object into a class type whose name is only known?

I have to dynamically fetch the tables whose names are available in dropdown in a jsp. Upon the selection of table name corresponding columns should be printed. For that I was running a loop in jsp and trying but is it possible to cast an object of "Object" type into a class whose class name is only known and after that using that object I have to acesss the corresponding class methods. ex: className I got from jsp is "Book" and I have a class Book.class which has a method getName(); so something like this is what I wanted: Object obj1 = Class.forName(className).cast(obj); obj1.getName(); Here obj is the object I have got through session.





Aucun commentaire:

Enregistrer un commentaire