Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java main() Method

The main() method in Java is the entry point of a standalone Java application. When you run a Java program from the command line or an integrated development environment (IDE), the Java Virtual Machine (JVM) executes the main() method. This tutorial will cover the syntax, usage, and behavior of the main() method in Java.

  • Syntax:

The main() method must be public, static, and have a return type of void. It takes a single argument, an array of strings (String[]), which represents the command-line arguments passed to the program.

Here's the basic syntax of the main() method:

public static void main(String[] args) {
    // Your program logic here
}
  • Location:

The main() method should be defined within a class or an interface with a static modifier (Java 8 onwards). Typically, you create a separate class with the main() method, but you can also place it within any class in your project.

public class MainClass {
    public static void main(String[] args) {
        // Your program logic here
    }
}
  • Accessing command-line arguments:

The args parameter of the main() method contains the command-line arguments passed to your program. You can access these arguments by indexing the args array.

public class CommandLineArgs {
    public static void main(String[] args) {
        System.out.println("Number of arguments: " + args.length);
        for (int i = 0; i < args.length; i++) {
            System.out.println("Argument " + (i + 1) + ": " + args[i]);
        }
    }
}
  • Execution:

To execute a Java program with a main() method, you need to compile the class containing the main() method and then run the compiled class using the java command.

For example, to compile and run the MainClass.java file:

javac MainClass.java
java MainClass

To pass command-line arguments to your program, provide them after the class name:

java CommandLineArgs arg1 arg2 arg3
  • Exit the program:

When the main() method finishes executing, the Java program terminates. You can also terminate the program explicitly using the System.exit() method, which takes an integer status code as an argument. A status code of 0 indicates normal termination, while non-zero values indicate an error.

public static void main(String[] args) {
    // Your program logic here

    System.exit(0); // Normal termination
}

This tutorial covered the basics of the main() method in Java, including its syntax, location, usage with command-line arguments, execution, and termination. The main() method is the entry point of your standalone Java application and provides a starting point for your program's execution.

  1. Creating a Java program with the main() method:

    • Every Java program starts with a main() method. It serves as the entry point for the program's execution.
    public class MyProgram {
        public static void main(String[] args) {
            // Program logic goes here
        }
    }
    
  2. Arguments and parameters in Java main() method:

    • The main() method can take an array of Strings as its parameter, representing command-line arguments.
    public static void main(String[] args) {
        // args is an array containing command-line arguments
        String firstArg = args[0];
    }
    
  3. Access modifiers for the main() method in Java:

    • The main() method is often declared as public, allowing it to be accessed from outside the class.
    public static void main(String[] args) {
        // ...
    }
    
  4. Launching Java applications with the main() method:

    • To launch a Java application, compile the source code and then run it using the java command with the class containing the main() method.
    javac MyProgram.java
    java MyProgram
    
  5. Overloading main() method in Java:

    • You can overload the main() method by defining multiple versions with different parameters. However, only the standard public static void main(String[] args) is the entry point for execution.
    public static void main(String[] args) {
        // Standard entry point
    }
    
    public static void main(String arg) {
        // Overloaded version, not used for entry point
    }
    
  6. Using command-line arguments in Java main() method:

    • Command-line arguments can be passed when running the Java program.
    public static void main(String[] args) {
        // args[0] contains the first command-line argument
    }
    // Run with: java MyProgram arg1 arg2