Java Reference
In-Depth Information
The only thing you need to make a Java application run, however, is one class that serves
as the starting point.
The class needs only one thing: a
main()
method. When the application is run, the
main()
method is the first thing called.
The signature for the
main()
method takes the following form:
public static void main(String[]
arguments
) {
//
body of method
}
Here's a rundown of the parts of the
main()
method:
public
means that this method is available to other classes and objects, which is a
form of access control. The
main()
method must be declared
public
. You learn
more about access methods during Day 6.
n
static
means that
main()
is a class method.
n
void
means that the
main()
method doesn't return a value.
n
main()
takes one parameter, which is an array of strings. This argument holds
command-line arguments, which you learn more about in the next section.
n
The body of the
main()
method contains any code you need to start your application,
such as the initialization of variables or the creation of class instances.
When Java executes the
main()
method, keep in mind that
main()
is a class method. An
instance of the class that holds
main()
is not created automatically when your program
runs. If you want to treat that class as an object, you have to create an instance of it in
the
main()
method (as you did in the
Passer
and
RangeLister
applications).
5
Helper Classes
Your Java application may consist of a single class—the one with the
main()
method—
or several classes that use each other. (In reality, even a simple tutorial program is actu-
ally using numerous classes in the Java class library.) You can create as many classes as
you want for your program.
NOTE
If you're using the JDK, the classes can be found if they are
accessible from a folder listed in your
Classpath
environment vari-
able.