110

I have a class and I want to find all of its public fields (not methods). How can I do this?

Joachim Sauer
309k59 gold badges567 silver badges624 bronze badges
asked Jan 24, 2010 at 10:38
1
  • 1
    You should be able to do this using Reflection API. Commented Jan 24, 2010 at 10:41

5 Answers 5

154
Field[] fields = YourClassName.class.getFields();

returns an array of all public variables of the class.

getFields() return the fields in the whole class-heirarcy. If you want to have the fields defined only in the class in question, and not its superclasses, use getDeclaredFields(), and filter the public ones with the following Modifier approach:

Modifier.isPublic(field.getModifiers());

The YourClassName.class literal actually represents an object of type java.lang.Class. Check its docs for more interesting reflection methods.

The Field class above is java.lang.reflect.Field. You may take a look at the whole java.lang.reflect package.

answered Jan 24, 2010 at 10:41
2
  • is this solution slow ? or can we use it freely ? Commented Dec 27, 2012 at 10:12
  • it's ok. But don't overuse it. Commented Dec 27, 2012 at 15:09
57
answered Sep 16, 2016 at 17:02
20

You can use any of the two based on your need:

Field[] fields = ClassName.class.getFields(); // returns inherited members but not private members.
Field[] fields = ClassName.class.getDeclaredFields(); // returns all members including private members but not inherited members.

To filter only the public fields from the above list (based on requirement) use below code:

List<Field> fieldList = Arrays.asList(fields).stream().filter(field -> Modifier.isPublic(field.getModifiers())).collect(
 Collectors.toList());
answered Oct 26, 2017 at 8:13
3

As mentioned by few users, below code can help find all the fields in a given class.

TestClass testObject= new TestClass().getClass();
Method[] methods = testObject.getMethods();
for (Method method:methods)
{
 String name=method.getName();
 if(name.startsWith("get"))
 {
 System.out.println(name.substring(3));
 }else if(name.startsWith("is"))
 {
 System.out.println(name.substring(2));
 }
}

However a more interesting approach is below:

With the help of Jackson library, I was able to find all class properties of type String/integer/double, and respective values in a Map class. (without using reflections api!)

TestClass testObject = new TestClass();
com.fasterxml.jackson.databind.ObjectMapper m = new com.fasterxml.jackson.databind.ObjectMapper();
Map<String,Object> props = m.convertValue(testObject, Map.class);
for(Map.Entry<String, Object> entry : props.entrySet()){
 if(entry.getValue() instanceof String || entry.getValue() instanceof Integer || entry.getValue() instanceof Double){
 System.out.println(entry.getKey() + "-->" + entry.getValue());
 }
}
answered Mar 22, 2017 at 21:50
1
  • Doesn't Jackson use reflection? Correct me if I am wrong Commented Mar 25, 2021 at 2:45
0

List fields=Arrays.stream(reservation.class.getDeclaredFields()).map(Field::getName).toList();

It returns the list of all members declared inside a class including the private members.

answered Feb 1, 2024 at 11:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.