站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Java Tutorial 5.0 英文版

The main Method - Java Tutorial 5.0 英文版

The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Getting Started
Lesson: A Closer Look at the "Hello World" Application

The main Method

The first bold line in the following listing begins the definition of the main method.
/** 
 * The HelloWorldApp class implements an application that
 * simply displays "Hello World!" to the standard output.
 */
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); //Display the string.
    }
}

In the Java programming language, every application must contain a main method whose signature looks like this:

public static void main(String[] args)
You can name the argument whatever you want, but most programmers choose "args" as shown above. Also, the modifiers public and static can be written in either order (public static or static public) but the convention is to use public static as shown above.

The method signature for the main method contains three modifiers:

  • public indicates that the main method can be invoked by any object. Controlling Access to Members of a Class (in the Getting Started trail) covers the ins and outs of the access modifiers supported by the Java programming language.
  • static indicates that the main method is a class method (as opposed to an instance method). Understanding Instance and Class Members (in the Getting Started trail) talks about class methods and variables.
  • void indicates that the main method doesn't return any value.

How the main Method Gets Invoked

The main method is similar to the main function in C and C++. When the interpreter executes an application, it starts by calling the class's main method. The main method then calls all the other methods required to run your application.

If you try to invoke the interpreter on a class that does not have a main method, the interpreter refuses to run your program and displays an error message similar to this:

In class NoMain: void main(String argv[]) is not defined

Arguments to the main Method

As you can see from the following code snippet, the main method accepts a single argument: an array of elements of type String.
public static void main(String[] args)
This array is the mechanism through which the runtime system passes information to your application. Each String in the array is called a command-line argument. Command-line arguments let users affect the operation of the application without recompiling it. For example, a sorting program might allow the user to specify that the data be sorted in descending order with this command-line argument:
-descending

The "Hello World" application ignores its command-line arguments, so there isn't much more to discuss here. However, you can get more information about command-line arguments, including the framework for a command-line parser that you can modify for your specific needs, in the Setting Program Attributes (in the Getting Started trail) lesson.


Note to C and C++ Programmers:  The number and type of arguments passed to the main method in the runtime environment differ from the number and type of arguments passed to C and C++'s main function. For further information refer to Command-Line Arguments (in the Getting Started trail) in the Setting Program Attributes (in the Getting Started trail) lesson.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.