I was wondering if the following scenario is possible.
Having two classes (Source and Destination) where in code I could do this:
public class Source{
private String fieldA;
private String fieldB;
public Source(){ ... }
}
...
public class Destination{
public Destination(Source src){ ... }
}
Source src = new Source();
Destination dest = new Destination(src);
dest.fieldA = "test";
dest.fieldB = "test";
So what I mean here is that I have two classes, one called Source
that contains (private) fields and one called Destination
with no fields. After creating two objects of these classes and passing in Source
into the constructor of Destination
, I want to be able to duplicate/copy the fields of Source
into Destination
.
Could something like this be possible in Java, whether or not using Reflection? And if possible, can someone give me a minor example I can start with.