Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Field API

Posted on 2011-02-15
9
Medium Priority
?
408 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 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
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.

 

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

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.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses

730 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