Does a getter with a different return type qualify as a getter?
Although the JDK allows this, it is not recommended to do so.
First, there are conventions based on the naming of your methods that have some dependencies on them.
-
JavaBeans use getters and setters to set bean properties. If they disagree with the agreement, the program will fail. See JavaBeans Specification, section 8.3.1
-
JSON Objects - The JSONObject class uses accessor methods in the constructor. See: http://www.json.org/javadoc/org/json/JSONObject.html#JSONObject(java.lang.Object )
-
Some JPA implementations will handle your entities incorrectly if they have multiple getters with the same name but different return types.
I'm sure there are more dependencies, but even if they don't apply to your solution, it's still wise to follow the convention for the sake of readability. You don't want any other developer to curse you sometime in the future for breaking standards :)
source to share
I think this is a bad idea from a design point of view.
I would just get a getter public List<String> getNames()
.
For "safe publishing", you should consider returning a copy of the list in the implementation:
public List<String> getNames() {
return new ArrayList<String>(names);
}
source to share
You are thinking about something else. The getXxx form method is a getter. Java conventions do not imply that you have to have a field named xxx. This is your business class, and encapsulation provides simple: the ability to hide your data structures to other classes.
Sorry for the typo, mobile.
source to share