Command-line Java applications are the simplest of all Java programs. Although they require very little overhead, command-line applications do require a
. The execution of an application begins with this method. You already know that the Java interpreter is used to run applications. When an application is executed within the Java interpreter, the interpreter calls the
method and executes the code within it. The following figure shows how the
Running a Java application means picking a particular class and passing its name as an argument to the Java virtual machine. When we did this, the java command looked in our HelloJava class to see if it contained the special method named main() of just the right form. It did, and so it was executed. If it had not been there, we would have received an error message. The main() method is the entry point for applications. Every standalone Java application includes at least one class with a main() method that performs the necessary actions to start the rest of the program.
Our main() method sets up a window (a JFrame) to hold the visual output of the HelloJava class. Right now, it is doing all the work in the application. But in an object-oriented application, we normally delegate responsibilities to many different classes.
In the next incarnation of our example, we are going to perform just such a split, creating a second class.
We will see that as the example subsequently evolves, the main() method remains more or less the same, simply holding the startup procedure.
Let us quickly walk through our main() method, just so we know what it does.
First, main() creates a JFrame, the window that will hold our example:
JFrame frame = new JFrame("Hello, Java!");
The word new in this line of code is very important. JFrame is the name of a class that represents a window on the screen, but the class itself is just a template, like a building plan. The new keyword tells Java to allocate memory and actually create a particular
JFrame object. In this case, the argument inside the parentheses tells the JFrame what to display in its title bar. We could have left out the "Hello, Java" text and used empty parentheses to create a JFrame with no title, but only because the JFrame specifically
allows us to do that.
When frame windows are first created, they are very small. Before we show the
JFrame, we set its size to something reasonable
frame.setSize( 300, 300 );
This is an example of invoking a method on a particular object. In this case, the set Size() method is defined by the JFrame class, and it affects the particular JFrame object we have placed in the variable frame. Like the frame, we also create an instance of JLa
bel to hold our text inside the window:
JLabel label = new JLabel("Hello, Java!", JLabel.CENTER );
JLabel is much like a physical label. It holds some text at a particular position, in this case, on our frame.
This is a very object-oriented concept: using an object to hold some text, instead of simply invoking a method to
draw
the text and moving on. The reasoning behind this will become clearer later.
Next, we have to place the label into the frame we created:
frame.add( label );
Here, we are calling a method named add() to place our label inside the JFrame. The JFrame is a kind of container that can hold things.
We will talk more about that later. main()'s' final task is to show the frame window and its contents, which otherwise would
be invisible. An invisible window makes for a pretty boring application.
frame.setVisible( true );
That is the whole main() method. As we progress through the examples in this chapter,
it will remain mostly unchanged as the HelloJava class evolves around it.
The
public
keyword is an
access modifier.
When the
public
keyword is applied to the
main()
method it means that the method is visible everywhere.
The
main()
method in the
Skeleton
application is
defined as static, which means that the member is associated with the class itself and not a specific instance of the class.
When you run this application in the Java interpreter, the
main()
method is called but there is no code to execute.
So,
main()
returns and the interpreter terminates the application.
The
main()
method only appears in Java applications, and not in applets. Applets have an
init()
method that is called to perform applet initialization, but an applet does not execute solely through the
init()
method in the same way that an application executes through the
main()
method. As you can see, the
main()
method is responsible for the start and finish of an application. The following figure shows how an application ends when the
main()
method finishes executing.
The first requirement in creating an executable Java application is to create a class with a method whose signature (name and method arguments) match the main method, defined as follows:
public class HelloExam {
public static void main(String args[]) {
System.out.println("Hello exam");
}
}
This main method should comply with the following rules:
- The method must be marked as a public method.
- The method must be marked as a static method.
- The name of the method must be main.
- The return type of this method must be void.
- The method must accept a method argument of a String array or a variable argument of type String.
It is valid to define the method parameter passed to the main method as a variable argument (varargs) of type String:
public static void main(String... args)
To define a variable
argument variable, the ellipsis (...) should follow the type of the variable and not the variable itself
(a mistake made by lot of new programmers):
The following method declaration will not compile.
public static void main(String args...)
The correct syntax for the Java `main` method to accept multiple arguments is as follows:
public static void main(String... args)
In this version:
- The `String... args` uses the correct varargs syntax, allowing the `main` method to accept zero or more `String` arguments.
- Varargs (`...`) must be placed after the type (`String` in this case), indicating that the method can accept any number of `String` arguments.
This is the standard way to declare the `main` method in Java.