Solved

Field API

Posted on 2011-02-15
9
399 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 …
This video teaches viewers about errors in exception handling.

636 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