Java Program Structure

Context: FIT2099_MOC · the skeleton every Java program shares · the entry point that runs a class · statically typed

Quick Revision

  • 🎯 Objective: recognise the mandatory anatomy of a Java program ➔ a public class (named like its file) with a main entry point.
  • ⚡ Key Constraint: main must be public static void main(String[] args)static lets the JVM call it without creating an object first; every statement ends in ;.

📝 Core

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}
  • public ➔ visibility — this member is callable from outside the class (see clients).
  • HelloWorld ➔ the class name must match the file name (HelloWorld.java).
  • main(...) ➔ the program’s entry point; String[] args are command-line arguments (java HelloWorld arg1 arg2).
  • static ➔ the JVM calls main without an object (before any instance exists).
  • void ➔ the return type “nothing”; System.out.println prints to the console.
  • ; ➔ Java is strict — every statement ends with a semicolon.

⚠️ Common Mistakes

  • 💡 File name ≠ class name = won’t compile ➔ a public class must live in a file of the same name.
  • 💡 Non-static main = JVM can’t start itmain must be static so it runs before any object is created.

🧠 Active Recall