Java Reference
In-Depth Information
Running Java Applications
The SCJP certifi cation exam tests your knowledge of running a Java program from the
command line using an appropriate
CLASSPATH
. If you are using Sun's Java Development Kit
(JDK), then
java.exe
in the
\bin
folder of the JDK directory is the executable used to run
your Java applications. The sample commands in this topic assume
java.exe
is in your path.
The entry point of a Java program is
main
, which you can defi ne in any class. The
signature of
main
must look like this:
public static void main(String [] args)
The only changes you can make to this signature are the name of the parameter
args
,
which can be arbitrary, and the order of
public
and
static
. For example, the following
declaration is a valid signature of
main
:
static public void main(String [] x)
In addition, you can specify the array of
String
objects using the syntax for variable-
length arguments:
public static void main(String... args)
Variable-Length Arguments
As of Java 5.0, a method in Java can declare a variable-length argument list denoted by
the ellipsis (
...
). Variable-length arguments are discussed in detail in Chapter 2.
The
args
array contains the command-line arguments, discussed in detail later in this
section. The
main
method has to be
public
so that the JVM has access to it, and making it
static
allows the JVM to invoke this method without having to instantiate an instance of
the containing class.
Let's start with a simple example. Suppose the following class is saved in the
c:\myproject
directory. First, does the following
SaySomething
class compile, and does it
successfully declare the
main
method?
1. public class SaySomething {
2. private static String message = “Hello!”;
3.
4. public static void main() {
5. System.out.println(message);
6. }
7. }
Search WWH ::
Custom Search