Anatomy of a program in Java
Classes, the main method signature, braces, statements and semicolons.
public 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");
}
}Your turn
One character is missing. What happens?
public class Hello {
public static void main(String[] args) {
System.out.println("Hi")
}
}Prints HiCompile errorPrints 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
public class Demo {
System.out.println("Hi");
}A class body can hold fields, methods, constructors and initializer blocks, but not bare statements.
public class Demo {
public static void main(String[] a) {
System.out.println("Hi");
}
}The statement lives inside a method.
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 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
- Code lives inside classes
- Classic entry point: public static void main(String[] args)
- Braces { } mark blocks; every statement ends with ;
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");
}- Main
- main
- start
- 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) {
}
}- No: statements must be inside a method or initializer block
- Yes, and it prints Hi when the class loads
- Yes, but nothing is printed
- 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.