how to get a constant in java with class

后端 未结 6 1461
一个人的身影
一个人的身影 2021-02-13 14:51

basically I need to get a constant for a class however I have no instance of the object but only its class. In PHP I would do constant(XYZ); Is there a similar way

6条回答
  •  轮回少年
    2021-02-13 15:24

    To get private constant like:

    private static final String BAR = "test";
    

    you need to set it accessible first:

    Field barField = Foo.class.getDeclaredField("BAR");
    barField.setAccessible(true);
    String bar = (String) barField.get(null);
    

提交回复
热议问题