如何从ClassName.class获取静态final属性?

huangapple go评论69阅读模式
英文:

How to get static final property from ClassName.class?

问题

I have array of classes like this.

    
    private static final Class<?>[] CLASSES = new Class[]{
        First.class,
        Second.class,
    };

Each class have property

        public static final String PROPERTY = "property_name";

Need to make loop to compare `PROPERTY` with specific string like this:

    for (Class<?> item : CLASSES) {
        string.equals(item.PROPERTY)
    }

But I couldn't find a way to escape from ".class" to get `item.PROPERTY`.

How to get `PROPERTY` in a correct way in this case?

Thanks!
英文:

I have array of classes like this.

private static final Class&lt;?&gt;[] CLASSES = new Class[]{
    First.class,
    Second.class,
};

Each class have property

    public static final String PROPERTY = &quot;property_name&quot;;

Need to make loop to compare PROPERTY with specific string like this:

for (Class&lt;?&gt; item : CLASSES) {
    string.equals(item.PROPERTY)
}

But I could't find a way to escape from ".class" to get item.PROPERTY.

How to get PEOPERY in a correct way in this case?

Thanks!

答案1

得分: 1

你应该使用:

for (Class<?> item : CLASSES) {
    Field f = item.getDeclaredField("PROPERTY");
    string.equals(f.get(item));
}
英文:

You should use:

    for (Class&lt;?&gt; item : CLASSES) {
        Field f = item.getDeclaredField(&quot;PROPERTY&quot;);
        string.equals(f.get(item));
    }

答案2

得分: 1

Did you mean how to deal with the exception?

public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
    Class<?>[] classes = new Class[] { ClassA.class, ClassB.class, };

    for (Class<?> item : classes) {
        Field f = item.getDeclaredField("PROPERTY");
        System.out.println(f.get(item));
    }
}

public static void main(String[] args) {
    Class<?>[] classes = new Class[] { ClassA.class, ClassB.class, };

    for (Class<?> item : classes) {
        try {
            Field f = item.getDeclaredField("PROPERTY");
            System.out.println(f.get(item));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
英文:

Did you mean how to deal with the exception?

enter code here
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
	Class&lt;?&gt;[] classes = new Class[] { ClassA.class, ClassB.class, };

	for (Class&lt;?&gt; item : classes) {
		Field f = item.getDeclaredField(&quot;PROPERTY&quot;);
		System.out.println(f.get(item));
	}
}
enter code here
public static void main(String[] args) {
	Class&lt;?&gt;[] classes = new Class[] { ClassA.class, ClassB.class, };

	for (Class&lt;?&gt; item : classes) {
		try {
			Field f = item.getDeclaredField(&quot;PROPERTY&quot;);
			System.out.println(f.get(item));
	 	} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

huangapple
  • 本文由 发表于 2020年8月10日 17:07:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63337241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定