Java Basics
Commentary: Method terminology?
Method = Function = Procedure = Subroutine = Subprogram
The word method is commonly used in Object-Oriented Programming and is used in Java. Every programming language has this idea, but sometimes uses other terms such as function, procedure, subroutine, ... Many programmers,including Java programmers, use these other terms, especially function, but these notes will use method, the official Java term.
Method categories
There have been proposals to establish a convention for identifying different categories of methods. This could be used in documentation for example. There are two categories that are well established.
- Getter - methods return (ie, get) a
value from an object. The value often exists as a simple
field in the class, but may also be synthesized when the getter
is called.
Synonym = Accessor. Those coming from a C++ background will be more familiar with equivalent term accessor.
Naming conventions. The Java naming conventions for getter methods depends on whether the returned value is boolean or not.
- Boolean getter names typically begin with the word
"is" or "has", as in
if (x.isVisible()) . . . while (y.hasDescendants()) . . .
- All other type getters have names beginning with
the word "get", as in
String greeting = z.getText()
- Boolean getter names typically begin with the word
"is" or "has", as in
- Setter methods set a value or property of an object, which
might be implemented as a field, but could be implemented in other ways.
Return value. Typically setters return no value, but two useful alternatives to void are (1) return the old value, and (2) return this object so that setter calls can be chained.
Synonym = Mutator. If you're a C++ programmer, you're probably familiar with the term mutator for this type of method, although in C++ mutator may apply to any function that modifies any aspect of an object.
Naming convention. Setter methods should begin with "set", for example,
z.setText("Hello Earthling");
- Other categories have been proposed, for example: factory methods, conversion methods, enumerators, listeners, testing, etc.