How i can access inner class variable value using

2019-08-14 04:20发布

问题:

Suppose i have class like below with multiple inner classes :

public class Constants {

    public class Class1 {
        public static final String IODATA = "IOData";
        public static final String HUB_INPUTS = "HubInputs";
    }

    public class Class2 {
        public static final String CLASS_INPUTS = "Class Inputs";
        public static final String AUXILIARIES_ON_BUTTON = "Auxiliaries On Button";
    }

    public class Class3 {
        public static final String CLASS_INPUTS = "Class Inputs";
        public static final String AUXILIARIES_ON_BUTTON = "Auxiliaries On Button";
    }
}

I want to access all inner class variable then i can write code like below and i can get the variable name and value:

public static void getClassNameMap() throws Exception  {
    Constants cons = new Constants();
    Constants.Class1 class1 = cons.new Class1();
    for (Field fields : class1 .getClass().getFields())  {
      System.out.println(fields.get(projectObjectCons).toString() + "--" + fields.getName());
    }
}

But some how i get array of String of all inner classes name like :

String []arr = {"Class1","Class2","Class3"};

how can i get all inner class variable and value using these String class name ??? Because i can not make class instance using its string name. Please help me out.

回答1:

Class[] classes = Constants.class.getDeclaredClasses();     
    for(Class innerClass: classes){
        //System.out.println(innerClass.getName());
        Field[] fields = innerClass.getDeclaredFields();
        for(Field field : fields){
            //System.out.println(field.getName());
            //your implementation
        }           
    }

This code will match your expectations.

Thanks



回答2:

Maybe this snippets is what you're trying to achieve...

    public static void main(String[] args)  {
        Class klass = Constants.class;
        Class[] cs = klass.getClasses();

        for(Class c : cs) {
            System.out.println(c.getName());
            Field[] fields = c.getDeclaredFields();
            for(Field f : fields) {
                try {
                    Object o = f.get(c);
                    System.out.println( "\t" + f.get(c).toString() + " -- " + f.getName());
                } catch (Exception e) {
                    // ignore others
                }
            }
        }
    }

If you have String to the class you can use

Class klass = Class.forName("com.package.of.Constant");

Hope it helps.



回答3:

I believe you are looking for something like this :

class Sample {

    public static void main(String arf[]) throws ClassNotFoundException {
        Class<?> clazz = Class.forName("C");
        Class<?>[] arr = clazz.getDeclaredClasses();
        System.out.println(arr.length);
        String[] classAsStrings = new String[arr.length];
        for (int i=0;i<arr.length;i++){
            classAsStrings[i] = arr[i].getSimpleName();
        }
        System.out.println(Arrays.toString(classAsStrings));
    }

}

class C {
    class Class1 {

    }

    class Class2 {

    }
}

O/P :

2
[Class1, Class2]