Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

main: practical reference

main: practical reference: The Java launcher invokes a public static void main(String[] args) method on the selected class after loading, linking, and class initialization.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

public static void main(String[] args)

The Java launcher invokes a public static void main(String[] args) method on the selected class after loading, linking, and class initialization.

Use main as the narrow process boundary: validate command-line arguments, assemble application dependencies, call ordinary methods, and decide the process exit policy.

main is static and receives an array supplied by the launcher; it is not an instance constructor. Uncaught exceptions terminate the initiating thread, while System.exit bypasses normal stack unwinding.

The args array can be empty but is not supplied as null by the standard launcher. Static initialization can fail before main begins, and overloaded methods named main are not alternative launcher entry points.

The example exercises the documented element in a small deterministic program and compares exact standard output.

Examples

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        System.out.println(args.length);
        System.out.println(Arrays.toString(args));
    }
}

Result

0
[]

The example exercises the documented element in a small deterministic program and compares exact standard output.

Sources