1. Technology

Main Method

By

Definition:

Every Java application must have a main method. It’s the starting point for the execution of the code in the application. Its method signature is:


public static void main(String[] args) 

The args parameter is a String array that contains any command-line arguments used to run the application.

Examples:

The program below has a main method that prints out any command-line arguments passed when running the code:


import java.util.Arrays;
public class PrintArguments {

    public static void main(String[] args) {
        String commandlineArgs = Arrays.toString(args);

        //The Arrays.toString function encases the Strings
        //in the array with square brackets.
        if (commandlineArgs.equals("[]"))
        {
            System.out.println("There were no arguments.");
        }
        else
        {
            System.out.println("There command-line arguments were: " + commandlineArgs + ".");
        }
    }

}

Glossary:

# A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

  1. About.com
  2. Technology
  3. Java
  4. Java Glossary
  5. M
  6. Definition for the Java Term: Main Method

©2014 About.com. All rights reserved.