0

I 've been read documents about clone object and my conclusion is clone objects is bad practice.

Is one of the best way to make a new object with the same values that another ?

Example:

    class Student {

    private String name;
    private int age;


    public String getName() {
        return name;
    }
    public void setName(String name){
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age){
        this.age = age;
    }
}

public class AnotherClass{

Student guy = new Student();
guy.setName("Mike");
guy.setAge(20);

Student guy2 = makeClone(guy); //now i get a new guy with the same values of the first guy


private Student makeClone(Student oldStudent){

    Student newStudent = new Student();
    newStudent.setName(oldStudent.getName());
    newStudent.setAge(oldStudent.getAge());

    return newStudent;

  }

}
  • 1
    To clone an object you must make copy of everything, because the attributes will be references between them. Now, about what is better or not I think that make an own clone method is better. – Ele Nov 10 '15 at 13:16
  • For reference: http://www.javapractices.com/topic/TopicAction.do?Id=71 – Andy Turner Nov 10 '15 at 13:17
  • Ty for the fast answer, i just what to know the best and most efficient way to clone. – Diogo Santos Nov 10 '15 at 14:41
  • Nowadays we got 1000 ways to do the same thing and someone mark this question possible duplicate from a question with 6 years .... I just want to know if exist new ways to clone objects or if some one have their own functions that make it easily. – Diogo Santos Nov 10 '15 at 14:44

0 Answers0