|
|
The "Hello World" Application |
The first bold line in the following listing begins the definition of amainmethod./** * 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. } }Every Java application must contain a
mainmethod whose signature looks like this:The method signature for thepublic static void main(String[] args)mainmethod contains three modifiers:
publicindicates that themainmethod can be called by any object. Controlling Access to Members of a Classcovers the ins and outs of the access modifiers supported by the Java language.
staticindicates that themainmethod is a class method. Instance and Class Memberstalks about class methods and variables.
voidindicates that themainmethod doesn't return any value.How the
mainMethod Gets CalledThemainmethod in the Java language is similar to themainfunction in C and C++. When the Java interpreter executes an application (by being invoked upon the application's controlling class), it starts by calling the class'smainmethod. Themainmethod then calls all the other methods required to run your application.If you try to invoke the Java interpreter on a class that does not have a
mainmethod, the interpreter refuses to compile your program and displays an error message similar to this:In class NoMain: void main(String argv[]) is not definedArguments to the
mainMethodAs you can see from the following code snippet, themainmethod accepts a single argument: an array of elements of typeString.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:public static void main(String[] args)-descending
Note to Mac OS JDK Programmers only: The JDK compiler for Mac OS allows an application that does not need to accept command-line arguments to declare itsmainmethod without any arguments, like this:
An application with such apublic static void main() { . . . }mainmethod will not prompt the user for command-line arguments.
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
lesson.
Note to C and C++ Programmers: The number and type of arguments passed to themainmethod in the Java runtime environment differ from the number and type of arguments passed to C and C++'smainfunction. For further information refer to Command Line Argumentsin the Setting Program Attributes
lesson.
|
|
The "Hello World" Application |