Field API

Is there a way to get the values from the getter method of a POJO through Field API java reflection?.
public class Cruft {
	public static void main(String[] args) {
		Student student = new Student();
		student.setAge(12);
		student.setName("Foo");
		Field[] fields = Student.class.getDeclaredFields();
		for (Field field : fields) {
			System.out.println(field.getType().getSimpleName() + field.getName() );
		}
	}
}

Open in new window

Micheal_MaleAsked:
Who is Participating?
 
for_yanCommented:

Are you talking about invocation of methods through reflection, like in this example:
http://www.javadb.com/invoke-methods-of-an-object-using-reflection
0
 
Gurvinder Pal SinghCommented:
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Micheal_MaleAuthor Commented:
Thanks. Figured out after reading Field api.
public class Cruft {
	public static void main(String[] args) {
		Student student = new Student();
		student.setAge(12);
		student.setName("Foo");
		Field[] fields = Student.class.getDeclaredFields();
		for (Field field : fields) {
			System.out.println(field.getType().getSimpleName() + field.getName() );
System.out.println(field.get(student));		
}
	}
}

Open in new window

0
 
Micheal_MaleAuthor Commented:
forgot to mention that i have to add :-

field.setVisible(true);

also in order to get the values
0
 
objectsCommented:
> from the getter method of a POJO

be aware that what you are doing is not calling the getter methods
0
 
Micheal_MaleAuthor Commented:
you are right objects but i did not find any easier way to get the values from the getter method through reflection when looping through the field name.
0
 
objectsCommented:
there are two examples of how to above
0
 
Micheal_MaleAuthor Commented:
Used method API which worked just fine
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.