Java Reference
In-Depth Information
APPENDIX
E
Running Java without BlueJ
Throughout this topic, we have used BlueJ to develop and execute our Java applications. There
is a good reason for this: BlueJ gives us tools to make some development tasks very easy. In
particular, it lets us execute individual methods of classes and objects easily; this is very useful
if we want to quickly test a segment of new code.
We separate the discussion of working without BlueJ into two categories: executing an applica-
tion without BlueJ and developing without BlueJ.
E.1
Executing without BlueJ
Usually, when applications are delivered to end users, they are executed differently than from
within BlueJ. They then have one single starting point, which defines where execution begins
when a user starts an application.
The exact mechanism used to start an application depends on the operating system. Usually,
this is done by double-clicking an application icon or by entering the name of the application
on a command line. The operating system then needs to know which method of which class to
invoke to execute the complete program.
In Java, this problem is solved using a convention. When a Java program is started, the name of
the class is specified as a parameter of the start command, and the name of the method is main .
The name “main” is arbitrarily chosen by the Java developers, but it is fixed—the method must
have this name. (The choice of “main” for the name of the initial method actually goes back to
the C language, from which Java inherits much of its syntax.)
Let us consider, for example, the following command, entered at a command line such as the
Windows command prompt or a Unix terminal:
java Game
The java command starts the Java virtual machine. It is part of the Java Development Kit (JDK),
which must be installed on your system. Game is the name of the class that we want to start.
The Java system will then look for a method in class Game with exactly the following signature:
public static void main(String[] args)
The method has to be public so that it can be invoked from the outside. It also has to be
static , because no objects exist when we start off. Initially, we have only classes, so static
 
 
Search WWH ::




Custom Search