How to get the list of all declared data variable of a class in another class including private variables [closed]

StackOverflow https://stackoverflow.com/questions/19178720

  •  30-06-2022
  •  | 
  •  

Question

How can I get the list of all declared data variable of a class in another class including private variables ? Thanks in advance

Was it helpful?

Solution

Try using Reflection in java.
Ex-

public class Abc {
    public static void main(String[] args) {
        A ob = new A();

        Field[] f;
        String[] s;
        f = ob.getClass().getDeclaredFields();

        System.out.println(f.length);

        for (int i = 0; i < f.length; i++) {
            System.out.println(f[i].getName());
        }
    }
}
class A {
    private int x = 10;
    String st = "hii";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top