Friday, 16 August 2013

Create Fields and methods dynamically

Create Fields and methods dynamically

I want to add new fields(variables) and encapsulating methods for a given
class. For example: A class name Student has no any fields like below:
public Class Student implements Serializable{
}
then in my application an instance is created;
Student s=new Student();
I want to add new methods which do not exist for student class at the run
time.for example : I want to add a field called studentName; and
getStudentName(); and setStudentName();
Then at the run time the studnet object will be like this;
public Class Student implements Serializable{
private String studentName;
public void setStudentName(..){}
public String getStudnetName(){return ...;}
}
In my application objects are written to a text file and all objects of
same type do not have all variables. Therefore, I want to add only the
required fields to save memory.
Any way, Is there a way to do this? any sample code or link ?

No comments:

Post a Comment