Class Methods
Objective #1: Call class methods.
- Since methods are reusable sections of code that perform actions, it makes sense to add methods to a class that you develop so that objects instantiated from the class can perform the actions consistency. For example, the Turtle class
has a forward method so a client program can move a Turtle object forward in a predictable way.
- To implement an individual method within a class means
to type code into the body of the method so the method knows how to perform when
it is called.
- Within a client program, you might declare an object variable
from a class and type the method name that you are calling after the name of
that
object variable as in
public class ExampleClient()
{
public static void main(String[] args)
{
Bug nemo = new Bug();
Bug willie = new Bug();
willie.setAge(2);
System.out.println("My horizontal position is " + getAge());
}
}
- Within a class, you may call another method within the same
class without having to type anything in front of the dot operator. Although,
you may optionally use the keyword this.
public void moveRight()
{
myX = getX() + 10;
}
or
public void moveRight()
{
myX = this.getX() + 10;
}
Here is another example of calling a method from within another method:
public void moveBackAndForth()
{
moveRight();
moveLeft();
}
public void moveRight()
{
myX = myX + 5;
}
public void moveLeft()
{
myX = myX - 5;
}
Notice again that you do not need an object variable name in front of the name of the method that you are calling. In other words, it does not make sense to type nemo.turnLeft(); since there is no object variable named nemo in the Bug class itself. An object variable named Bug would likely only exist in a separate client program.
Objective #2: Pass parameters to methods.
- One or more parameters may be passed to a method.
The identifiers x and y in the following move method
are explicit parameters. More specifically, they are sometimes called formal
parameters as
well since their names are simply formal placeholders that only store values
passed to them.
public void move(int x, int y)
{
myX = myX + x;
myY = myY + y;
}
A value or variable identifier that is found in the call statement that calls
a
method from a client program such as the values 20 & 30 in the following statement
is called
an actual
parameter (and sometimes called an argument).
nemo.forward(20);
- A whole object variable may even be passed as a parameter!
The following method
public void move(Bug another)
{
myX = another.getX();
myY = another.getY();
}
could be called with the call statement
another = 30;
nemo.move(30);
where nemo is an implicit parameter & another is
a formal parameter.
Objective #3: Be able to use static methods
- Regular methods that we have already studied such as shoot or move explained above or even accessor or modifier methods such as getX or getY are more specifically called instance methods. There is another type of method called static methods. Static methods are sometimes called class methods.
- The keyword static is used in the method header and the method applies to the whole class rather than a specific object (i.e. instance of a class).
- A method is a static method if the keyword static is used in the method's header. Here is an example of a static method
public static void displayAreaFormula()
{
System.out.println("area of rectangle = length * width");
}
- You call a static method by typing the name of the class followed by the dot operator and the name of the method. You do not call a static method with the name of a specific object variable.
- Example
public class Rectangle
{
// properties, constructors, accessors, & modifiers
public static void displayAreaFormula()
{
System.out.println("area of rectangle = length * width");
}
}
public class RectangleTest
{
public static void main(String[] args)
{
Rectangle.displayAreaFormula();
}
}
An error would occur if you replaced the main method with this code....
Rectangle box = new Rectangle();
box.displayAreaFormula();
Objective #4: Miscellaneous aspects of methods not covered earlier in this course.
- In addition to int and double primitive variables, you
can also use an object variable as a local variable.
Here is an example:
public void shoot()
{
Tank tankTemp = new Tank();
tankTemp.move(30, 50);
myX = tankTemp.myX;
myY = tankTemp.myY;
}
The object variable tankTemp is local to the method shoot. Furthermore, it is
legal to use an object in the implementation of one of its own methods! It is
also legal to reference tankTemp's private myX property since this code is found
within the class itself. However, using an accessor as in myX
= tankTemp.getX(); is legal as well.
- Be careful not create a side effect when writing an accessor method. One type of side effect is a logic error that modifies a property of a class in addition to returning
a property's value.
- Constants are declared by placing
the keyword final before
the usual declaration. For example, you
might use the statement
final int PI =
3.1415926;
which declares a constant named PI that
can be used over and over again
without fear of accidentally changing the value of PI to
something else. A constant is also useful
since the name DOZEN is much easier to recognize
and reuse than typing out the number 3.1415926. Constants should be named
with all-capital letters for good style and underscores should be used to
separate multiple words as in SPEED_OF_LIGHT
Within a class such as the Tank class, a constant would be declared near
the properties of the class with the statement
private final static int MAX_AMMO = 25;
In this example, the constant is made private so that MAX_AMMO can only be
used within the Tank class. However, it is sometimes useful to use public rather than private so that the constant can be used in client programs,
test classes, and other classes. The static keyword is used so that there
is only one copy of MAX_AMMO rather than one copy per Tank object that happens
to be instantiated at a given moment. We will study more about this use of static later.
- You can have two different implementations of a method with the same name in the same class. In this case, the method name is overloaded and the two (or more) versions of the method are called overloaded methods. For example,
public void moveRight()
{
myX = myX + 5;
}
and
public void moveRight(int amount)
{
myX = myX + amount;
}
are both valid since the one version accepts no parameters are the other version requires an int parameter. The method signature of a method is its name along with its parameter list (which is the list of parameters such as int in the case of the second version above). The method signatures of these two methods are: moveRight() & moveRight(int amount). Since the method signatures of the methods are different, they can use the same name moveRight.