java Reflection Question - I am lost.
Please compile and run this example. Please read step by step comments to understand what I expect to achieve from this sample program.
Code :
import java.util.*;
import java.lang.reflect.*;
//I first create a simple local class
class MyClass1
{
public Double aDouble1;
public Double aDouble2;
public MyClass1()
{
aDouble1 = 250.0;
aDouble2 = -100.9;
}
}
public class Identifier
{
public static void main(String [] args)
{
//OK. I create an instance of MyClass1
MyClass1 m1 = new MyClass1();
//Then I get the runtime Class of that object
Class<?> that = m1.getClass();
//Then I get all the declared fields of that object
Field [] fields = that.getDeclaredFields();
//I define an Object array to the size of Field array
Object[] fieldsAsObjects = new Object[fields.length];
//Now I get each Field object into the Object array
try
{
for (int next = 0; next < fields.length; next++)
{
fieldsAsObjects[next] = fields[next].get(m1);
}
}
catch(Exception exp)
{
exp.printStackTrace();
}
//Since now I have fields of m1 object in the array, I go change
//then to a new value (or rather make them zeros).
for (int next = 0; next < fieldsAsObjects.length; next++)
{
fieldsAsObjects[next] = 0.0;
}
//Afterwards I EXPECT THE m1.aDouble1 and m1.aDouble2 to be changed
//to 0.0. But it is not happpening. Why and how to?
System.out.println(m1.aDouble1);
System.out.println(m1.aDouble2);
}
}
Re: java Reflection Question - I am lost.
Hello there,
I tried running your code and this is what I've come up with, you cant really just grab the objects and change them like that. Even though it returns an object, its actually a FieldAccessor, I dont really know how this works deep down but you just cant do it that way. Luckily though there is another way of changing the field value.
See below.
Code :
import java.lang.reflect.Field;
//I first create a simple local class
class MyClass1 {
public Double aDouble1;
public Double aDouble2;
public MyClass1() {
this.aDouble1 = 250.0;
this.aDouble2 = -100.9;
}
}
public class ReflectionTest1 {
public static void main(final String[] args) {
// OK. I create an instance of MyClass1
final MyClass1 m1 = new MyClass1();
// Then I get the runtime Class of that object
final Class<?> that = m1.getClass();
// Then I get all the declared fields of that object
final Field[] fields = that.getDeclaredFields();
// Loop through the fields and call the set method to change it and
// pass in the object on which you want to alter the field on
try {
for (int next = 0; next < fields.length; next++) {
fields[next].set(m1, 0.0);
}
} catch (final Exception exp) {
exp.printStackTrace();
}
// Afterwards I EXPECT THE m1.aDouble1 and m1.aDouble2 to be changed
// to 0.0. But it is not happpening. Why and how to?
System.out.println(m1.aDouble1);
System.out.println(m1.aDouble2);
}
}
As you can see here, I use the field.set() method to set a new value on that field on object m1. Hope that makes any sense.
// Json
Re: java Reflection Question - I am lost.
Hi Json,
Thanks. And surely it makes sense and cute way.
Thanks again!
prain
Re: java Reflection Question - I am lost.
No problem. Lets get this thread marked as solved then :)
// Json