Video in progress

Junior ☕ Core Java

Why does the main method look like public static void main(String[] args)?

The JVM calls it without creating an object (static), from outside the class (public), doesn't expect a result (void), and passes the command-line arguments as a String[]. In Java 21+ a simpler void main() also appeared, as a preview.

📖 Answer

public class App {
    public static void main(String[] args) {
        System.out.println("Argumentlar soni: " + args.length);
    }
}
java App.java hello 42   # args = ["hello", "42"]

Why each part is there:

  • public: the JVM calls it from outside the class;
  • static: when the program starts, there is no object yet;
  • void: the exit code is set through System.exit(code);
  • String[] args: the command-line arguments, always strings; you convert a number yourself with Integer.parseInt.

The String... args (varargs) form works too.

What's new: "compact source files" and instance main methods, a preview in Java 21-24 and final in Java 25, for beginners:

void main() {
    IO.println("Salom");
}

This is handy for teaching and small scripts; real projects keep using the classic form.