Consider this
public class Data {
private final SomeField[] fields;
.....
public SomeField[] getFields() {
return map == null ? null : map.
The utility method Arrays.copyOf(T[] original, int newLength) will create a new array with the same objects from the internal array.
The issue with return the internal array is usually about preventing unintended changes to the contents of the array, which would be shared any other clients. Sharing the contained objects is not usually of the same order of concern but if you are implementing some sort of map your requirements may be more stringent.
To solve this problem you must avoid to user ternary operator. Instead of this, you must use if operator.
Example:
public CustomMap[] getMap() { CustomMap[] obj = null;
if (map != null){
obj = map.clone();
}
return obj;
}
OR
public CustomMap[] getMap() {
CustomMap[] obj = map == null ? null : map.close();
return obj;
}
I solve my problem using the abouve code. I think that is mandatory to create a new object explicit. I think.
Regards!