myMethod Report(Report report){
Report copyofreport = report;
return copyofreport;
}
would give me a new object, an instance of the class Report.
It kind of worked, but introduced a subtle bug. I tried calling this method multiple times, thinking I could get an array of identical reports- but when I did something to my first report, all of the 'copies' I had made were similarly affected, even though I hadn't touched them.
What this actually did was make an array of references that all pointed to my original object, report. when I changed one, the references all reflected the same change.
solution: to make a new, identical object, there's a handy interface called cloneable. implement cloneable, override the method if necessary, and then call .clone().
// public class Report implements Cloneable;
ReportmyMethod Report(Report report){
Report copyofreport = report.clone();
return copyofreport;
}
Much better.
This only creates a 'shallow copy' though, so if there are any inner objects in the cloneable class, the objects will have the same reference. This really only matters if you're going to change the inner objects, but it's good to be aware of.