In Java, can one force a method to return by value (not by reference)?
For example:
public Rect getBounds() {
return bounds; //TODO: Return bounds by value so users can't change edit it.
}
Here I might as well just make bounds public. There seems to be no reason to create a function to return something if its going to pass it by reference.
My other option is to create several methods return the specific members of bounds 1-by-1 but I would rather not.
No. This is why APIs often return defensive copies, unmodifiable wrappers, etc.
These copies and/or wrappers (help) prevent users from modifying instance data.
If correct behavior relies on instances having full, and sole, control over instance data, instances are responsible for ensuring that behavior by not giving clients anything they can use to break that contract.