Yes, it does nothing except returning the same reference that has been passed.
Probably, you are looking for the Object#clone()
* method:
public Validation copy(Validation newValidation) {
return newValidation.clone();
}
In 95% cases, that is the most appropriate and proper solution for different types of tasks. You should not reinvent the wheel if there isn't a need of it.
As the Java documentation says:
... this method performs a "shallow copy" of this object, not a "deep copy" operation.
... it may be necessary to modify one or more fields of the object returned by super.clone
before returning it.
Validation y = x.copy(x);
You shouldn't pass an x
to a copy
method because when you are calling this method on an x
instance, inside the class you have an access to this
which, in this case, represents your x
.
Validation y = x.clone();
For a "shallow copy" the preceding example is good, but for a "deep copy" you need to override a default Object#clone()
behaviour:
class A implements Cloneable {
public @Override A clone() throws CloneNotSupportedException {
return (A)super.clone(); // or or another implementation
}
}
class Validation implements Cloneable {
// an example of a reference-type field
private A a; // with a setter
public @Override Validation clone() throws CloneNotSupportedException {
Validation newClone = new Validation();
// note that the `A` class has to implement the `Cloneable`
// and override the `clone` method making it `public`
newClone.setA(this.a.clone());
return newClone;
}
}
*Don't forget to implement the Cloneable
interface to allow cloning.
To read: Effective Java, Item 11: Override clone judiciously.