reflection - how to get a constant in java with class -


Basically I need to get a constant for a square, but I do not have any instance of the object, But only in the class I will do the constant (XYZ) in PHP; Is there a similar way of gaining a consistency in Java?

I need this to facilitate a dynamic getMethod call

  class parameter type = class. ForName (class_name); Object.setProperty (field name, field value, type parameter);  

The set property method must then obtain the correct method and set the specified asset, although I can not get a way that is int for the parameter type without using the interface.

I'm not sure what you want to achieve but it will show you an example. Should not be difficult.

Indicate that you have Class Fu with property bar.

  square fu {private finals string bar = "test"; Public string getBar () {return bar}}}  

Now to get it through reflection:

  class fooClass = Foo.class; Object fooObj = fooClass.newInstance (); Method fooMethod = fooClass.getMethod ("getBar"); String bar = (string) fooMethod.invoke (fooObj);  

You will now get the value of getBar () in the bar variable


Comments