DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Jean-pierre has posted 6 posts at DZone. View Full User Profile

Obtaining The Value Of A Public Static Field By Reflexion

09.06.2007
| 2407 views |
  • submit to reddit
        // Obtaining the value of a static public constant of a class, here Boolean for example
        Object returnedObj = null;
        try {
            Class clazz = Class.forName("java.lang.Boolean");
            returnedObj = clazz.getField("TRUE").get(clazz);
            assert java.lang.Boolean.TRUE == returnedObj : "wrong returned object";
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return returnedObj;