Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling the class in java, using the name of a class from a string

Tags:

java

class

Is there any easy way to achieve this.

Let's say I have an array of string with the name of POJO's in there and I"m trying to print all the list of attributes, is there any way to achieve this easily?\

String [] nameofClass;
for(String name:nameofClass)
name.class.getDeclaredFields();

Thanks

like image 940
R Syed Avatar asked Jan 24 '26 21:01

R Syed


1 Answers

Class.forName(name).getDeclaredFields() within your for loop is probably what you're looking for.

Note that name should be the full path of the class, i.e. not only TheClass but the.package.to.TheClass.

like image 52
sp00m Avatar answered Jan 27 '26 11:01

sp00m