Never been to DZone Snippets before?

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

Obtaining the value of a public static field by reflexion (See related posts)

// 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;

You need to create an account or log in to post comments to this site.


Click here to browse all 4858 code snippets

Related Posts