I have a function that returns a user-defined object. First I want to know if that object is returned by reference and what if it was private?
Also, how do I return it as Constant (final) reference because I don’t want someone to mess with it? I’m so confused between returning an object and returning object.copy(); or object.clone();
In Java, You always return a reference (unless returned value is a primitive type such as
int,float,char, …).So, if you don’t want the returned object to be modified, you must return a full copy of it (you could use
Clonableinterface andclonemethod if your class defines it).