Getting a list of all declared fields and values in an Object

Use the reflection API. It allows you to inspect class members at runtime. The
process is simple: get the classname from the class you want to inspect and invoke
getDeclaredFields. This will give you an array of member variables that have
been declared in that object. To get the actual value, call the get method on
a field instance with the object you want to inspect.

You are not allowed to inspect private values, unless you specifically ask for it
(see second example).

Person.java (the class we are inspecting)

public class Person
{
   public String name;
   private int age;
 
   public Person(String name, int age) {
      this.name = name;
      this.age = age;
   }
}

Main.java

import java.lang.reflect.Field;
  
public class Main
{
   public static void main(String[] args)
   {
      Person p = new Person("John Doe", 36);
 
      try {
         listObjectMembers(p);
      }
      catch(Exception e) { 
         System.out.println(e);
      }
   }
 
   private static void listObjectMembers(Object o) {       
      if (o == null) return;
        
      Class cl = o.getClass();
      Field fields[] = cl.getDeclaredFields();
      for (int i=0; i<fields.length; i++) {
         Field field = fields[i];
         Class fieldType = field.getType();            
 
         System.out.println(fieldType.getName() + " " + field.getName());
 
         try {
            Object member = field.get(o);
            System.out.println("thas value: " + member);
         }
         catch(IllegalAccessException e) {
            System.out.println("t" + e);     
         }        
      }
   }
}

Running this program will yield:

java.lang.String name
	has value: John Doe
int age
	java.lang.IllegalAccessException: Person

As the integer “age” is declared as being private to the class, the Java Runtime
Environment will throw an IllegalAccessException. However, since Java 2, a new
function has been added to the Field class that solves this problem.
Invoke setAccessible(true) on a field that is private, and you’ll get the value.

import java.lang.reflect.Field;
  
public class Main
{
   public static void main(String[] args)
   {
      Person p = new Person("John Doe", 36);
 
      try {
         listObjectMembers(p);
      }
      catch(Exception e) { 
         System.out.println(e);
      }
   }
 
   private static void listObjectMembers(Object o) {       
      if (o == null) return;
        
      Class cl = o.getClass();
      Field fields[] = cl.getDeclaredFields();
      for (int i=0; i<fields.length; i++) {
         Field field = fields[i];
         Class fieldType = field.getType();            
 
         System.out.println(fieldType.getName() + " " + field.getName());
 
         try {
            field.setAccessible(true);
            Object member = field.get(o);
            System.out.println("thas value: " + member);
         }
         catch(IllegalAccessException e) {
            System.out.println("t" + e);     
         }        
      }
   }
}

outputs:

java.lang.String name
	has value: John Doe
int age
	has value: 36

See the Reflection API for more functionality.