Java Reference
In-Depth Information
This application takes one or more command-line arguments and displays them in all
uppercase letters. Here's an example of running the program and the resulting output:
java Passer Athos Aramis Porthos
ATHOS ARAMIS PORTHOS
The
Passer
application uses command-line arguments stored in the
arguments
array of
strings.
The application creates a
Passer
object and calls its
toUpperCase()
method with the
arguments
array as an argument (lines 10-11).
Because a reference to the array object is passed to the method, changing the value of
each array element in line 5 changes the actual element (rather than a copy of it).
Displaying the array with lines 12-14 demonstrates this.
Class Methods
The relationship between class and instance variables is directly comparable to how class
and instance methods work.
Class methods are available to any instance of the class itself and can be made available
to other classes. In addition, unlike an instance method, a class does not require an
instance of the class for its methods to be called.
For example, the Java class library includes the
System
class, which defines a set of
methods that are useful when displaying text, retrieving configuration information, and
accomplishing other tasks. Here are two statements that use its class methods:
5
System.exit(0);
“”int now = System.currentTimeMillis();
The e
xit(
int
)
method closes an application with a status code that indicates success (
0
)
or failure (any other value). The
currentTimeMillis()
method returns a
long
holding
the number of milliseconds since midnight on Jan. 1, 1970, the numeric representation of
the current date and time.
To define class methods, use the
static
keyword in front of the method definition as you
would in front of a class variable. For example, the class method
currentTimeMillis()
in the preceding example might have the following signature:
static void exit(int arg1) {
// body of the method
}