I want to write a java program to print all (public or private) parameters of a class and their types. I think I have to use reflection, but I’m noob at java reflection.
As an example, I need the my program to run on below class and result in following output:
class a{
public int b;
public int c;
private String s;
}
output:
b: int
c: int
s: St
Finally my question is how to get a list of parameters of a class and their types.
I would recommend you explore the javadoc, starting with the
Classclass:Of note is the method
getDeclaredFields(), which returns an array ofFieldobjects representing the fields the class declares.Also take note of the ways to obtain a
Classobject: