CHAPTER 5: Introduction to Java: Objects, Methods, Classes, and Interfaces 139
You can see a good example of using void in a method that “triggers” something. This type of
method would be used to trigger (invoke) a change in the object, and thus would not need to send
any specific data values back to its calling entity.
If your method or function returns a data value, then instead of using the void keyword, you would
use the data type of the data value that needs to be returned, say int or String. As an example,
a simple, whole number addition method might return a number data value after finishing its sum
calculation, so you would declare a data type using the int keyword.
After the data type keyword comes a name for the method (say, shiftGears). This is followed by
the type of data (in this case, an int) and variable name (newGear) in parentheses, which is called the
parameter list, and then finally the curly braces, which will contain the method’s Java code, just like
we used with the Java class:
void shiftGears (int newGear) { The Java code which defines the method's functionality will go in
here. }
The data variable’s data type and name, seen within the parameter list, contains the data parameter
that will be passed into the method, so the method now has this passed-in data variable to work
with inside of the Java code that is defined inside of the method programming logic (inside of its
curly braces).
Note The normal method-naming convention is to start a method name with a lowercase letter, and
then to use uppercase letters to begin words embedded within the method name, called CamelCase, like
this: methodNameExample( ). Read more about naming conventions for Java at:
http://www.oracle.com/technetwork/java/codeconv-138413.html.
Some methods, such as those that trigger something, will be “called” without using any variables,
as follows:
turnCarOff();
To call the .shiftGears() method, you would want to pass the desired gear over using the parameter
list as an integer data variable, so you would therefore utilize the following method call format:
shiftGears(4);
This passes over the integer value of 4 using the .shiftGears() method’s newGear variable, which sets
its value. This data value is then utilized in the interior of the .shiftGears() method logic (the part inside
the curly braces), where it is finally used to set the object’s gear (internal) field to the new gear shift value
of 4, or fourth gear. If you wanted to set up your .shiftGears( ) method so that it does not require any
integer data values, that is, if you wanted to set it up to be a method with no calling parameter, you will
need to create a .shiftGearUp( ) as well as a .shiftGearDown( ) method. The programming logic inside of
these methods would add (or subtract) a value of one from the current gear setting, instead of setting the
gear value to the passed in (desired) gear value. In Java coding, there’s always more than one way to skin
a car! Or is it, there’s always more than one way to shift a cat?