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.

Example:
public String ReturnConcat (myObject objMYObject)
{

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


log.String(x)

}
ScottTigerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jerchCommented:
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++)
            System.out.println(fields[i].getName());
    }
   
    public static void main(String[] args) {
                       
        ListProperties lp = new ListProperties();
        lp.listProps(lp);
    }
}

Hope this helps.

Jerson
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ScottTigerAuthor Commented:

I am very pleased by your answer, but am looking for obtaining the actual value for the properties, not the names.
0
jerchCommented:
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)

Jerson
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.