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: 
You could use Java reflection to analyze all fields of a given object and print them with their values. For more information, look at the reflection doc: 
<http://docs.oracle.com/javase/tutorial/reflect/member/index.html>
Each object should be responsible for representing itself when printed. Here is how you accomplish that:

```
class MyClass {
    int myValue;
    public MyClass(){myValue = 2}; // constructor
    public String toString() {
        return "value " + myValue;
    }
}

```

Whenever you want to print your class from this point on, simply say

```
MyClass mc = new MyClass();
System.out.println(mc);

```