Retrieving all properties for an object

I want to accept and object and loop through all of its properties.
I am looking for a method to do this regardless of the object.
My goal is to make a string of the properties.

public String ReturnConcat (myObject objMYObject)

this.<loop through properties> (objMyObject);
String(x) = String(x) + String(y)


Who is Participating?
jerchConnect With a Mentor Commented:
This includes public, protected, default (package) access, and private fields, but excludes inherited fields.

If you want to only get the public fields, change getDeclaredFields() to getFields().

import java.lang.reflect.*;

class ListProperties {
    public int property1;
    int property2;
    protected int property3;
    private int property4;
    public void listProps(Object object) {
        java.lang.Class objectClass = object.getClass();
        Field[] fields = objectClass.getDeclaredFields();
        for (int i = 0; i < fields.length; i++)
    public static void main(String[] args) {
        ListProperties lp = new ListProperties();

Hope this helps.

ScottTigerAuthor Commented:

I am very pleased by your answer, but am looking for obtaining the actual value for the properties, not the names.
I don't think you can do that with ordinary Java object except for Javabean.  If you really want to do that, all properties of the object should have a getter method which is in the form of getXXX() where XXX is the property name starting with a capital letter.  For example, if the property name is width, you should have corresponding getWidth() method. That's what we call introspection.  In Javabeans, it is standard to provide the setter and getter methods but if you want to have a method with different name you can do so by providing BeanInfo (which I'm not familiar with)

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.