How to read the fields in the class to find out what type

Asked 2 years ago, Updated 2 years ago, 117 views

Can you examine the class of instances that a variable has in Java? I would like to read the fields one by one and check the type.

class java field

2022-09-22 16:13

1 Answers

You can use reflect.

import java.lang.reflect.Field;
//...
SomeClass targetClass = new SomeClass();

for(Field field : targetClass.getClass().getDeclaredFields()){
    System.out.println(field.getType()+" "+field.getName());
}

You can do it.


2022-09-22 16:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.