Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Field API

Posted on 2011-02-15
9
Medium Priority
?
414 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 300 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:Gurvinder Pal Singh
Gurvinder Pal Singh earned 300 total points
ID: 34903798
0
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 34903817
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

926 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