☕ Java Foundations · Beginner

Anatomy of a program in Java

Classes, the main method signature, braces, statements and semicolons.

🧩 The mysterypublic static void main(String[] args): that's a lot of ceremony just to say hi. Let's decode every piece.

The classic shape

Java code lives inside a class. Inside it, a method named main is where the program starts: the JVM finds it and runs its statements top to bottom.

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hi");
    }
}

Braces and semicolons

Braces { } group statements into a block: the class body, the method body. A statement is one instruction, like a println call, and every statement ends with ;. The semicolon is Java's full stop.

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hi");
    }
}
🔮 Predict it

Your turn

One character is missing. What happens?

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hi")
    }
}
  1. Prints Hi
  2. Compile error
  3. Prints Hi with a warning
Show the answer

Compile error. The println line has no semicolon, so javac reports ';' expected and refuses to build the program.

Exactly main

The java launcher searches for a method named exactly main. Java is case-sensitive: a method called Main, start or run won't be found. (String[] args holds any words typed after the program name on the command line.)

Statements need a home

✗ Won't compile
public class Demo {
    System.out.println("Hi");
}

A class body can hold fields, methods, constructors and initializer blocks, but not bare statements.

✓ Works
public class Demo {
    public static void main(String[] a) {
        System.out.println("Hi");
    }
}

The statement lives inside a method.

🤔 Think first

Russian dolls

Order these from the outside in: method, statement, class, block.

Think about it, then reveal the answer

Class → method → block → statement. Classes contain methods, a method's body is a block, and blocks contain statements, each ending in ;.

💼 In the real world

In real projects

Your IDE flags a missing ; or brace instantly with a red squiggle. Knowing the anatomy lets you read the error fast: ';' expected or reached end of file while parsing tell you exactly which piece is missing.

Key takeaways

  1. Code lives inside classes
  2. Classic entry point: public static void main(String[] args)
  3. Braces { } mark blocks; every statement ends with ;
🤯 Did you know?

main can also be written public static void main(String... args). The ... (varargs) form means the same as String[], and the launcher accepts it.

Practice questions

Which word completes the classic entry point?

public static void ___(String[] args) {
    System.out.println("Hi");
}
  1. Main
  2. main
  3. start
  4. run
Check your answer

main. The launcher looks for a method named exactly main. Java is case-sensitive, so Main is a different name.

Does this compile?

public class Demo {
    System.out.println("Hi");
 
    public static void main(String[] args) {
    }
}
  1. No: statements must be inside a method or initializer block
  2. Yes, and it prints Hi when the class loads
  3. Yes, but nothing is printed
  4. Only if the class is not public
Check your answer

No: statements must be inside a method or initializer block. A class body can hold fields, methods, constructors and initializer blocks, but not bare statements like a println call.

Next: print, println, printf. Three ways to talk to the screen, and a separate channel just for errors.