I have this question. But it will be difficult for me to explain as I don't know exact terms to use. Hope someone will understand. I'll try to discribe to the best i can. I feel like this is much related to parsing
Say there are two classes. And in both classes I have some variables, say strings (just for simplicity, variable type can be any), which have similar names.
Eg:
class ClassA{
String x,y,z;
}
class ClassB{
String x,y,z;
}
Now, what i need is, i need to copy the value of one class's variable values to other classes corresponding variable.
Eg:
ClassA aa=new ClassA();
ClassB bb=new ClassB();
//set bb's variables
aa.x=bb.x;
aa.y=bb.y;
aa.z=bb.z;
like that.
But please note that what i need is not the above method. I hope there will be a way to write a simple method, so that it will identify the relevent variable by the name passed to it. Then it will do the value assignment accordingly.
My imagined method is like this,
void assign(String val){
// aa.<val>=val
}
For example if you pass bb.x
to assign(...)
method, then it will do aa.x=bb.x
assignment.
Hope this is clear enough. There must be a better way to explain this. If someone know it please edit the post(+title) to make it more clear (But save my idea)..
Please let me know if there's a way to achieve this.
Thanks!
why not share a common use-case with us
? (I saw you changing your answer into a comment :p) – Homeopathistidentify the passed variables name
and deal with a variablewith the same name
in some other independent object (both variables will be of the same type). – Homeopathistreflection
. I haven't come across that.. (You'd better put it as an answer, as this doesn't like extended discussions :D) – Homeopathist