fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.lang.reflect.*;
  7.  
  8. class Foo {
  9. public static final String Bar = "Bar";
  10. }
  11.  
  12. /* Name of the class has to be "Main" only if the class is public. */
  13. class Ideone {
  14. public static void main (String[] args) throws java.lang.Exception
  15. {
  16. Object value = null;
  17. String constName = "Bar";
  18. Field constant = Foo.class.getField( constName );
  19.  
  20. if ( constant != null ) {
  21. value = constant.get( null );
  22. }
  23.  
  24. if ( value != null ) {
  25. System.out.println( value );
  26. } else {
  27. System.out.println( "Constant " + constName + " was not found." );
  28. }
  29. }
  30. }
  31.  
Success #stdin #stdout 0.05s 4386816KB
stdin
Standard input is empty
stdout
Bar