Methods that include the word static in their declaration are
called static methods.
The purpose of a static method is to accomplish one well-defined
task, allowing someone to call that method without having to deal with
all of the details of how that task is accomplished. Static
methods used in a program don't have to be in the same class. If
a static method is in the same class as the program that is using it, a
call to that method just includes the name of the method, parentheses,
and any arguments that are needed. For example, if this method is
in a class:
public
static void printMsg(String
msg) {
System.out.println(msg);
}
and you want to use that method to print a message somewhere else in
that class, it would look like this:
printMsg("The number is prime");
We've already seen, however, cases where we call
static methods that are not in the same class. One example:
num = Stdin.readInt();
The name of the method being called in that
statement is readInt(). That method is in a class called
Stdin. The Stdin class is in the file hsa.jar that we have
included in our projects. If
you are writing a program in a class called NumberProgram (in a file
called NumberProgram.java), and you have the printMsg() method in
another class called Primes (the code for which is in a file called
Primes.java), you can call the printMsg() method in the Primes class
like this:
Primes.printMsg("The number is
prime");
The only difference is before the name of the
method, there is the name of the class that contains that method
(Primes), and a dot. This will work as long as the Primes class
is in the same package as the class that wants to call its
methods. This is not the case for the Stdin class, which is
why any class that calls methods in the Stdin class has to have
this line at the beginning of the file:
import hsa.Stdin;
The name of the package that includes the Stdin
class is hsa,
and it includes a number of classes that come with our textbook, Java for AP Computer Science.
That package is inside of a compressed archived named hsa.jar.
Java code is generally organized this way. You can
put multiple methods that perform related tasks into one class, which
does not even need to include a main() method (which would make it a
program). A class with nothing but static methods that are meant
to be called from other programs is called a utility class. Since calls to
static methods begin with the name of the class that contains them,
static methods are also called class
methods.
Static, or class methods are not the only kind of
methods that are used in Java. Classes in Java can be one of
three types. Java applications are classes that have a main()
method. Utility classes are classes with static methods, but no
main() method. Classes are also used to define object data
types. To define a data type, a class has to provide a name for
the data type (which is the name of the class), provide a way of
creating objects of that data type, and define the operations that can
be performed on objects that have that data type.
One object data type that is built into Java is the
String data type. It is defined in a class called String.
Every String object you create in a program (whether it's a literal or
stored in a variable) is called an instance
(- an object that exists when a program is running whose data type was
defined by a class) of the String class. In general, every object
is an instance of the class that defined its data type. Those
classes define the operations that can be performed on instances by
providing a method for each operation. Those methods are not
static, and are not called by giving the name of the class.
Instead, the call to these methods starts with an instance of the class
(then followed by a dot the name of the method, as before). The
methods, when called, are said to be called
on the object, or instance, that comes before the dot.
Because of this, this type of method is called an instance method.
Remember that the operators that are used with
primitive data types (+, -, ==, !=, >, <, etc) cannot be used
with objects (with the exception of the + operator for Strings), which
is why methods have to be provided to define operations. For
example, if you wanted to determine whether two ints have the same
value, you can do this:
int x, y;
x = 3;
y = 4;
if (x == y)
where x == y is a boolean expression that will be
true if x and y have the same value, and false if not. For
Strings, you would have to do this instead:
String x, y;
x = "Hello";
y = "there";
if (x.equals(y))
where x.equals(y) is a boolean expression that will
be true if Strings x and y contain the same sequence of
characters. That method call as an expression has a boolean
value, because the equals() method in the String class returns a
boolean. Notice that the method is called on an instance of the
String class (x) rather than the String class itself. The full
declaration for the method looks like this:
public boolean equals(Object anObject)
and you can tell it is an instance method because
the declaration does not have the word static.
You can also see the difference in the Javadoc for the String class
which shows this for the equals() method in the Method Summary:
boolean |
equals(Object anObject)
Compares
this string to the specified object. |
which you can contrast to the documentation for the
class method prime() from the Primes class:
static boolean |
prime(int number)
Determines
if a number is prime |
which shows that prime() is a static method.
The data type for the argument to String's equals()
method is Object, which means that the equals() method will accept any
object as an argument, no matter what class the object is an instance
of. The only data types the argument cannot be are primitive data
types.
private
public
data member (field)
instance
static
client
constructor
accessor
mutator
name refers to object
anonymous object
references
null
static vs dynamic memory allocation
garbage collection