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

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Obtaining the value of a public static field by reflexion

// Obtaining the value of a static public constant of a class, here Boolean for example
   1  
   2          Object returnedObj = null;
   3          try {
   4              Class clazz = Class.forName("java.lang.Boolean");
   5              returnedObj = clazz.getField("TRUE").get(clazz);
   6              assert java.lang.Boolean.TRUE == returnedObj : "wrong returned object";
   7          } catch (ClassNotFoundException e) {
   8              e.printStackTrace();
   9          } catch (SecurityException e) {
  10              e.printStackTrace();
  11          } catch (NoSuchFieldException e) {
  12              e.printStackTrace();
  13          } catch (IllegalArgumentException e) {
  14              e.printStackTrace();
  15          } catch (IllegalAccessException e) {
  16              e.printStackTrace();
  17          }
  18  
  19          return returnedObj;
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS