how to get a constant in java with class

后端 未结 6 1294
一个人的身影
一个人的身影 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:34

    You might look for sth. like
    Foo.class.getDeclaredField("THIS_IS_MY_CONST").get(null); or Class.forName("Foo").getDeclaredField("THIS_IS_MY_CONST").get(null); (thanks f-o-o)

    Gets the value of a String constant (THIS_IS_MY_CONST) in class Foo.

    Update use null as argument for get thanks acdcjunior

提交回复
热议问题