Java reflection: get class of field

java, reflection

Solution

You need `Field#getType()`.

Returns a `Class` object that identifies the declared type for the field represented by this `Field` object.

`getClass()` is a method inherited from `Object` that returns the type of the object itself.

Problem

I have a method that requires something like `Double.class` as one of its inputs; e.g. ``` someOutput = SomeObj.someMethod("parameter",Double.class); ``` I have another class that has a bunch of fields of different datatypes that I'd like to feed as input to this method in stead of explicitly writing `Double.class` of `Integer.class` etc etc. I know this involves java reflection, but I'm not quite sure how to do what I want. I'm trying something like this but it isn't working: ``` for(Field field : Class.forName(MyClassWithLotsOfFields.class.getCanonicalName()).getFields()){ someOutput = SomeObj.someMethod("parameter",field.getClass()); //Do more stuff here... } ``` but `field.getClass()` is not giving me the actual class of the field from `MyClassWithLotsOfFields` but instead `java.lang.reflect.Field` any ideas for how to do what I want?

Original source