The main() Method (Java)


To totally unlock this section you need to Log-in


Login

A Java application (like DateApp in the code listed below) must contain a main() method whose signature looks like this:

public static void main(String args[])

import java.util.Date;
class DateApp {
    public static void main(String args[]) {
        Date today = new Date();
        System.out.println(today);
    }
}

The method signature for the main() method contains three modifiers:

  • public: indicates that the main() method can be called by any object. Other access modifiers supported by the Java language are: private, protected, and the implicit, friendly.
  • static: indicates that the main() method is a class method.
  • void: indicates that the main() method has no return value.

The main() method in the Java language is similar to the main() function in C and C++. When you execute a C or C++ program, the runtime system starts your program by calling its main() function first. The main() function then calls all the other functions required to run your program.

Similarly, in the Java language, when you execute a class with the Java interpreter, the runtime system 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 run a class with the Java interpreter that does not have a main() method, the interpreter prints an error message.

Arguments to the main() Method

As you can see from the code snippet above, the main() method accepts a single argument: an array of Strings.

public static void main(String args[])

This array of Strings 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.

Another Example

In Java, you need to have a method named main in at least one class. The following is what must appear in a real Java program.

public static void main(String [ ] args)
{
      UrRobot Karel = new UrRobot(1, 1, East, 0);
            // Deliver the robot to the origin (1,1),
            // facing East, with no beepers.
      Karel.move();
      Karel.move();
      Karel.move();
      Karel.pickBeeper();
      Karel.turnOff();
}

This method must appear within a class, but it can be any class. So here is the above main method enclosed in a class called Temporary. This would normally be in a file named Temporary.java.

Summary

A Java package is a way to collect different parts of a large program together logically. The Java system (JDK or Java Development Kit) comes with several packages of its own, but you can create your own.

In Java the qualifier public means that something is available across packages. It is also possible to restrict visibility of names like Temporary to a single package or even more, but here we make it public.

The particular form of main is required by Java. While the following explanation of its parts is not needed now, and probably not especially understandable, you can return to it later when you know more.

In Java, main is a static method. This means the method is part of its class and not part of objects. They are defined by classes, which are like factories for the creation of objects.

Strings in Java are sequence of characters, like the characters in this sentence. The matched brackets indicate that an array of Strings is required. An array is a certain kind of linear collection of things. The name args is just a name for this array. This name is the only part of the declaration of main that can vary.

The declaration (String [] args) is in parentheses as part of the declaration of main to indicate that when the class Temporary (in the above example) is executed by your computer's operating system, an array of strings can be sent to the program to help with program initialization. Since they are not referenced again in the body of main (the part between braces {...}) they won't be used here. The parentheses, like the rest, are required. There is no space between [ and ].

Braces { and } are used to collect statements into a "block" in Java and in some other programming languages. Statements in Java end with semicolons.

To execute the above program you first need to translate it into a machine readable form using the Java compiler. Then you need to use the Java run-time to execute the machine readable version. When you do this you give the run time the name of the class that contains your main method.