Java Get All Field Names of Class
Tags: Java reflect
In this Java tutorial we learn how to implement an utility class to programmatically get all field names of the class and supper classes of an object in Java.
Table of contents
- Example Java classes to get field names
- Implement Utility Class to get All Field Names
- How to use the ClassUtils class
Example Java classes to get field names
For example, we have two class BaseEntity and Customer which Customer class extends the BaseEntity class as below.
BaseEntity.java
public class BaseEntity {
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
Customer.java
public class Customer extends BaseEntity {
private String name;
private String email;
private String phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
If we have an instance of Customer class as below.
Customer customer = new Customer();
And we want to programmatically get list of field names of the customer object then we can do next step.
Implement Utility Class to get All Field Names
In order to get the all fields of an object we need to get all classes related to it including super classes as following Java code.
List<Class> listOfClasses = new ArrayList<>();
listOfClasses.add(classToGetFields);
Class superClass = classToGetFields.getSuperclass();
while(superClass != null && superClass != Object.class) {
listOfClasses.add(superClass);
superClass = superClass.getSuperclass();
}
From the above listOfClasses result we can loop it and get the list of field names.
Below is the final ClassUtils utility class.
ClassUtils.java
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class ClassUtils {
public static List<String> getAllFieldNames(Class classToGetFields) {
List<String> result = new ArrayList<>();
List<Class> listOfClasses = new ArrayList<>();
listOfClasses.add(classToGetFields);
Class superClass = classToGetFields.getSuperclass();
while(superClass != null && superClass != Object.class) {
listOfClasses.add(superClass);
superClass = superClass.getSuperclass();
}
for(Class aClass : listOfClasses) {
for(Field field : aClass.getDeclaredFields()) {
result.add(field.getName());
}
}
return result;
}
}
How to use the ClassUtils class
The following Java program to show you how to use the above ClassUtils class to get field names of an object.
FieldNamesExample1.java
import java.util.List;
public class FieldNamesExample1 {
public static void main(String... args) {
Customer customer = new Customer();
List<String> allFieldNames = ClassUtils.getAllFieldNames(customer.getClass());
allFieldNames.forEach(System.out::println);
}
}
name
email
phone
id
More example Java program to retrieve all field names of the BaseEntity class.
FieldNamesExample2.java
import java.util.List;
public class FieldNamesExample2 {
public static void main(String... args) {
BaseEntity baseEntity = new BaseEntity();
List<String> allFieldNames = ClassUtils.getAllFieldNames(baseEntity.getClass());
allFieldNames.forEach(System.out::println);
}
}
id
Happy Coding 😊