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-2 of 2 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
        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;

classmethod and staticmethod

class K(object):

  # normal method (instance method)
  def method1(self):
    print 'obj.method1() becomes method1(obj)'

  # class method
  def method2(cls):
    print 'K.method2() becomes method2(K)'
  method2 = classmethod(method2)

  # static method
  def method3():
    print 'K.method3() become just method3()'
  method3 = staticmethod(method3)

obj = K()
obj.method1()
K.method2()
K.method3()
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS