Clone() é um método definido em Object.
Como ele é protegido (protected) vc não terá acesso direto pelo seu objeto. Vc deve subscrevê-lo lá na sua classe, lembrando de alterar a visibilidade para public, se convir.
Nele vc deve copiar os valores para um novo objeto, que será retornado.
Algo mais ou menos assim.
public class Aluno {
private String nome;
private int idade;
public Object clone() throws CloneNotSupportedException {
Aluno aluno = new Aluno();
aluno.setIdade(this.idade);
aluno.setNome(this.nome);
return aluno;
}
//gets, sets, construtores..
}
e
//...
public static void main(String[] args) {
Aluno aluno1 = new Aluno("Fulano", 66);
Aluno aluno2 = aluno1; //ambos apontam para o mesmo lugar na memória!
Aluno aluno3 = null;
try {
aluno3 = (Aluno) aluno1.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
aluno1.setNome("Sicrano");
System.out.println("Nome de aluno1 = " + aluno1.getNome());
System.out.println("Nome de aluno2 = " + aluno2.getNome());
System.out.println("Nome de aluno3 = " + aluno3.getNome());
}
E a saída desse programa vai ser…
Nome de aluno1 = Sicrano
Nome de aluno2 = Sicrano
Nome de aluno3 = Fulano
Sacou?!
Tem uns links aqui…
<a href="http://java.sun.com/javase/6/docs/api/java/lang/Object.html#clone()" class="onebox" target="_blank">http://java.sun.com/javase/6/docs/api/java/lang/Object.html#clone()</a>
<aside class="onebox wikipedia">
<header class="source">
<a href="https://en.wikipedia.org/wiki/Clone_(Java_method)" target="_blank">en.wikipedia.org</a>
</header>
<article class="onebox-body">
<h3><a href="https://en.wikipedia.org/wiki/Clone_(Java_method)" target="_blank">clone (Java method)</a></h3>
clone() is a method in the Java programming language for object duplication. In Java, objects are manipulated through reference variables, and there is no operator for copying an object—the assignment operator duplicates the reference, not the object. The clone() method provides this missing functionality.
Classes that want copying functionality must implement some method to do so. To a certain extent that function is provided by "<code>Object.clone()</code>".
clone() acts like a copy constructor. Typic...
Espero ter ajudado agora…
[]s