Sunday, September 1, 2013
In our vlast posts we only concentrated on sniffing class declaration and its members. But this time we will go beyond it and try to get and set field values of a given class using Java Reflection.For this we just need an instance of a class. Though here we will deal with public fields only and someone might think that we can get and set field values in thes cases directly using instance of that class. But this is not the actual case in real situation. This is done only in places where directly we cannot set thopse values as in case of private members. So here we have used method getDeclaredField() (we will deal with private members later). Here we will just show that once you have the class refernce and the instance of a particualr class you can get and set the field values whether its an integer,float,string or array. There are seperate methods for different primitive data types like getInt() and setInt() for int , getLong() and setLong() for long and so on. But for data types other than primiotive ones you have to use get() and set() methods.
      Here in our demo we jave created a class Person with fields of type int, String and a String array. So you will be able to uinderstand clearly how to use different methods depending on data type.
-------------------------------------------------------------------------------------------------------------------------
Java Source Code
-------------------------------------------------------------------------------------------------------------------------
import java.lang.reflect.Field;
import java.util.Arrays;

import static java.lang.System.out;

class Person{
public int id=1521;
public String name="Luther";
public String hobbies[]={"cricket","music"};
}

public class FieldChanger {
public static void main(String[] args) {
try{
    Class c=Person.class;  //getting class reference
    Person pobj=new Person();
    
    Field id=c.getDeclaredField("id");  //getting field reference
    out.println("BEFORE :   id = "+id.getInt(pobj));  //getting initial int value
    id.setInt(pobj, 1926);  //setting a new int value
    out.println(" AFTER :   id = "+id.getInt(pobj));
    
    Field name=c.getDeclaredField("name");
    out.println("BEFORE :   name = "+name.get(pobj));  //getting initial String value
    name.set(pobj, "Jennifer");  //setting a new String value
    out.println(" AFTER :   name = "+name.get(pobj));
    
    Field hobbies=c.getDeclaredField("hobbies");
    //showing initial array values
    out.println("BEFORE :   hobbies = "+Arrays.asList(pobj.hobbies));
    String[] nhob={"football"};
    hobbies.set(pobj, nhob);  //setting new values for array
    out.println(" AFTER :   hobbies = "+Arrays.asList(pobj.hobbies));
    
}catch(NoSuchFieldException|
IllegalAccessException e){ }
}
}
-------------------------------------------------------------------------------------------------------------------------
Output
-------------------------------------------------------------------------------------------------------------------------
BEFORE :   id = 1521
 AFTER :   id = 1926
BEFORE :   name = Luther
 AFTER :   name = Jennifer
BEFORE :   hobbies = [cricket, music]
 AFTER :   hobbies = [football]
-------------------------------------------------------------------------------------------------------------------------
Download Links
-------------------------------------------------------------------------------------------------------------------------

Happy coding :)

0 comments:

Post a Comment

Total Pageviews

Subscribe via Email

Followers


Popular Posts

About Me

My photo

I am a student of BTech Computer Science Engineering from RCCIIT,Kolkata. I am a crazy lover of Java and wants to settle as a Java developer. I have a seven years Java experience with an application developer experience for 2 years. Recently from March 2012 I am a registered S40 app developer for Nokia and has corrected an app of them. I am currently writing blogs to encourage and grow interest in all those who don't know or learning Java.