Solved

Field API

Posted on 2011-02-15
9
378 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:Micheal_Male
9 Comments
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 75 total points
ID: 34903789

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
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 75 total points
ID: 34903798
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 34903817
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:Micheal_Male
ID: 34903904
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
 

Author Comment

by:Micheal_Male
ID: 34903908
forgot to mention that i have to add :-

field.setVisible(true);

also in order to get the values
0
 
LVL 92

Expert Comment

by:objects
ID: 34903956
> from the getter method of a POJO

be aware that what you are doing is not calling the getter methods
0
 

Author Comment

by:Micheal_Male
ID: 34904009
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
 
LVL 92

Expert Comment

by:objects
ID: 34904139
there are two examples of how to above
0
 

Author Comment

by:Micheal_Male
ID: 34906561
Used method API which worked just fine
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question