In fact, in java, technically everything is passed by value because the link is the value of the memory address, however you can think of it as sending by reference.
As far as performance is concerned, I doubt that there is a measurable difference, because the JVM JIT compiler is likely to be built into the accessor method ("getter"). In terms of style, it is better to use getters, preferring to publish your fields.
As for secure publishing (allowing secure access to personal data) - no, the object is not "locked in read-only mode"; it is fully editable because arrays are mutable.
To safely allow access to your data array, basically you have two options:
- returning a copy of the array from your getter is expensive
- provides an API that returns an element at a given position - easy to code, but it may be harder to resize the array later because the API was defined for your class
An example of providing an API might be:
public int getStructureOne(int x, int y, int z) { return structureOne[x][y][z]; }
This method is completely safe, because primitives (for example, int ) are passed by value - this caller changes the value of the variable to which the result of this method is assigned, nothing happens to the array.
source share