I have a POJO that has access to a method that returns an object of the same type (usually with some fields populated). Is it possible to implement a method in the POJO that calls this getter method and sets the instance of the POJO to reference the object that is returned? I would like to avoid setting a copy() function for every POJO that I create.
For example:
public class DAO {
public static BaseEntity get(int id) {
// returns some POJO
}
}
public abstract class BaseEntity {
public void get(int id) {
BaseEntity entity = DAO.get(id);
// sets "this" to the entity
}
}
public class POJO extends BaseEntity {
int foo;
String bar;
}
pojo.get(123)to populate its fields.