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.
-------------------------------------------------------------------------------------------------------------------------
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){ }
}
}
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 :)
Labels:Reflection
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
0 comments:
Post a Comment