Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Java program entry point

Java program entry point: The standard Java SE entry point is public static void main(String[] args); spelling the parameter as String... args has the same launcher signature.

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

Syntax

program entry

The standard Java SE entry point is public static void main(String[] args); spelling the parameter as String... args has the same launcher signature.

Keep main as a thin boundary: parse arguments, configure dependencies, invoke application code, and translate the result into a clear process status.

A main with wrong case, without public or static, or with another return type may compile but is not the launcher entry point; overloads do not fix it.

The JVM loads, links, and initializes the class before invoking main; args is an empty array when no arguments are supplied, and returning does not end the process while non-daemon threads run.

The launcher invokes main with no arguments, so stdout starts with args=0; daemon=false identifies a regular thread, then worker and joined show controlled waiting.

Examples

import java.util.concurrent.CountDownLatch;

public class Main {
    public static void main(String... args) throws InterruptedException {
        var gate = new CountDownLatch(1);
        var worker = new Thread(() -> {
            try {
                gate.await();
                System.out.println("worker");
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
        });
        worker.start();
        System.out.println("args=" + args.length);
        System.out.println("daemon=" + worker.isDaemon());
        gate.countDown();
        worker.join();
        System.out.println("joined");
    }
}

Result

args=0
daemon=false
worker
joined

The launcher invokes main with no arguments, so stdout starts with args=0; daemon=false identifies a regular thread, then worker and joined show controlled waiting.

Sources