Question: Is there a way that I can print out all names and values of an `Object` where `Object` can be of any class?

**i.e.** If `Object` was `Person` and `Person` had a member variable `Integer age;` - **I need a method** that could receive an instance of `Object` (where the `Object` is `Person` in this case but could be any other type of object) and print out `age - 35` where `age` is the member variable `name` and `35` is its `value`.

Something like:

```
public Map<String, String> getNamesAndValues(Object object) {
  Map<String, String> namesAndValues = new HashMap<>();
  //Populate namesAndValues Map here using object in signature.
  return namesAndValues;
}

```

If variables are `private` or `protected` then it should check for `getters` as well.

Answer: 
Here's a try, that use spring [`ReflectionUtils`](http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/ReflectionUtils.html).
NB: this rely on the fact that "value" have a proper string representation and it's not recursive (i.e. 1-level depth) 

```
package com.stackoverflow.so22075052;

import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class F {
    public static void main(final String[] args) {
        // fill a list
        final List<Object> objects = new ArrayList<>();
        objects.add(A.of(1));
        objects.add(B.of("2"));

        // introspect
        for (final Object object : objects) {
            ReflectionUtils.doWithFields(object.getClass(), new ReflectionUtils.FieldCallback() {
                @Override
                public void doWith(final Field field) throws IllegalArgumentException, IllegalAccessException {
                    ReflectionUtils.makeAccessible(field);
                    System.out.printf("%s - %s\n", field.getName(),  ReflectionUtils.getField(field, object));
                }
            });
        }
    }

    static class A {
        private int age;

        public static A of(final int age) {
            final A a = new A();
            a.age = age;
            return a;
        }
    }

    static class B {
        private String string;

        public static B of(final String string) {
            final B b = new B();
            b.string = string;
            return b;
        }
    }
}

```

Output:
-------

```
age - 1
string - 2

```

---

EDIT:

[BeanUtils#describe](http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.1/apidocs/org/apache/commons/beanutils/BeanUtils.html#describe%28java.lang.Object%29) and a `Map` might be another solution
```
        List<Object> list = new ArrayList<Object>();

        for (Object object : list) {

            if(object instanceof Person) // Cross check that object contain Person object
            {
                Person person = (Person) object;

                System.out.println("Person age "+person.age());

            }

        }

```