I have a class name (as a String), and I want to get all members and their types. I know I need to use reflection, but how?
For instance, if I have
class MyClass {
Integer a;
String b;
}
how do I get the types and names of a and b?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If the classes have already been loaded by the jvm, you can use the static method of Class called Class.forName(String className); and it will return you a handle to the reflections object.
you would do:
To get a variable named b from MyClass, one might do.
and if b is type integer, to get its value i would do.
or