THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

1.8.3. Static or Class Methods


Just as you can have per-class static fields, you can also have per-class static methods, often known as class
methods. Class methods are usually intended to do operations specific to the class itself, usually on static
fields and not on specific instances of that class. Class methods are declared using the static keyword and
are therefore also known as static methods.


As with the term "field", the word "method" generally means a per-object method, although the term
non-static method is sometimes used for clarity.


Why would you need static methods? Consider the Sony Walkman factory again. The record of the next serial
number to be assigned is held in the factory, not in every Walkman. A method that returned the factory's copy
of the next available serial number would be a static method, not a method to operate on specific Walkman
objects.


The implementation of distance in the previous example uses the static method Math.sqrt to calculate
a square root. The Math class supports many methods that are useful for general mathematical manipulation.
These methods are declared as static methods because they do not act on any particular instance of the Math
class; instead, they group a related set of functionality in the class itself.


A static method cannot directly access non-static members. When a static method is invoked, there's no
specific object for the method to operate on, and so no this reference. You could work around this by
passing an explicit object reference as an argument to the static method. In general, however, static methods
perform class-related tasks and non-static methods perform object-related tasks. Asking a static method to
work on object fields is like asking the Walkman factory to change the serial number of a Walkman hanging
on the belt of a jogger in Golden Gate Park.


1.9. Arrays


Simple variables that hold one value are useful but are not sufficient for many applications. A program that
plays a game of cards would want a number of Card objects it could manipulate as a whole. To meet this
need, you use arrays.


An array is a collection of variables all of the same type. The components of an array are accessed by simple
integer indexes. In a card game, a Deck object might look like this:


public class Deck {
public static final int DECK_SIZE = 52;
private Card[] cards = new Card[DECK_SIZE];
public void print() {
for (int i = 0; i < cards.length; i++)
System.out.println(cards[i]);
}
// ...
}


First we declare a constant called DECK_SIZE to define the number of cards in a deck. This constant is
public so that anyone can find out how many cards are in a deck. Next we declare a cards field for
referring to all the cards. This field is declared private, which means that only the methods in the current
class can access itthis prevents anyone from manipulating our cards directly. The modifiers public and
private are access modifiers because they control who can access a class, interface, field, or method.

Free download pdf