Everyone opens the first chapter of the OCP with the same thought: I know this already. That is exactly why people lose points there.

The exam doesn't test whether you can write Java. It tests what the compiler accepts. On that ground, daily experience is useless: in real life nobody writes String args[]. The exam loves it.

So before every example below, ask yourself one question: does this compile?

Every example here was compiled and run on JDK 21.0.13. The error messages are the real ones, copied from the terminal.

Part one — the file

Three rules govern what a .java file may hold. They belong to the file, not to the class inside it, and that distinction is where most mistakes start.

Rule 1 — at most one public class, and it names the file

A file may contain several classes. Only one of them may be public, and its name must match the file name exactly.

// File: Account.java public class Account { } // OK class Operation { } // OK: not public, it can live here too 

Make the second one public and the compiler objects:

// File: Account.java public class Account { } public class Operation { } 
Account.java:2: error: class Operation is public, should be declared in a file named Operation.java public class Operation { } ^ 1 error 

Read that message closely. It never says two public classes are forbidden. It says Operation, being public, belongs in Operation.java. Same rule seen from the other side — and stated that way, it is much easier to remember.

Rule 2 — no public class means the file name is free

The case everyone forgets. A file with no public class at all is perfectly valid, and you may call it anything.

// File: whatever.java class Account { } class Operation { } // Compiles 

Rule 3 — the header order is fixed

Three things may appear before the types, and they come in this order: the package, then the imports, then the types themselves. Swap any two and the file won't compile.

import java.util.List; package banking; public class Bad { } 
Bad.java:2: error: class, interface, enum, or record expected package banking; ^ 

This error message is worth a second look as well. The compiler doesn't say package in the wrong place. By the time it reaches line 2 it has already left the header behind, so it is expecting a type declaration and finds a keyword it cannot use there. The message describes what it wanted, not what you did wrong.

One freedom inside the class

All of the above constrains the file. Inside a class, member order is entirely free — a field can be declared after the method that uses it:

public class Demo { public static void main(String[] args) { System.out.println(field); } static String field = "declared after the method, and it works"; } 

That asymmetry is the thing to carry away: strict outside, free inside.

Part two — the main method

Only three constraints: public, static, and a void return. Everything else is negotiable.

What compiles and runs

static public void main(String[] args) { } // order of modifiers is free public static void main(String[] arguments) { } // parameter name is free public static void main(String args[]) { } // brackets after the name public static void main(String... args) { } // varargs 

All four compile, and all four run. I checked each one.

What compiles but never starts

Here is the part that catches people. The JVM looks for one exact signature; if it can't find it, the failure happens at runtime, not at compile time.

public void main(String[] args) { } // no entry point public static int main(String[] args) { } // no entry point public static void main(int[] args) { } // no entry point 

Drop the static and the JVM tells you exactly what it wanted:

Error: Main method is not static in class M4, please define the main method as: public static void main(String[] args) 

Notice where that message comes from: the JVM, not javac. The file compiled without a single complaint. Two different programs ran, and only the second one objected.

This matters beyond trivia. A multiple-choice question can legitimately offer "compiles, but fails at runtime" as an answer — and if you only ever ask yourself whether something compiles, you will never pick it.

The args[0] trap

Arguments arrive without the program name, unlike C. So args[0] is the first real argument — and if it isn't there, you get an exception, not null:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 

The null answer is tempting and wrong, for a simple reason: the array exists, it is merely empty. An empty array is not a null array. That distinction comes back throughout the exam.

Worth remembering

  • Two public classes in one file — does not compile; the second belongs in its own file
  • A file with no public class — compiles, and its name is free
  • import before package — does not compile
  • static public void main — compiles and runs; modifier order is free
  • String args[] and String... args — both compile and both run
  • public void main(String[] args) — compiles, but the JVM refuses to start it
  • args[0] with no argument passed — exception, not null
  • A field declared after the method using it — compiles; inside a class, order is free

How to work through this

Don't read these rules. Type them.

Everything above can be checked in thirty seconds in an empty file, and a compiler message you saw with your own eyes fades far more slowly than a rule you read. That is the whole reason this series uses a plain editor and javac rather than an IDE: an IDE would have underlined half these examples before I ever pressed compile, and I would have learned nothing from them.

This is part one of a series on the Building Blocks chapter. It follows the setup article, which covers installing JDK 21 on Windows, macOS and Linux.

Next: packages and imports — where an explicit import beats a wildcard one, and where an ambiguity is only reported at the point you actually use the name.

I'm preparing the 1Z0-830 exam with the OCP Java SE 21 Developer Study Guide (Sybex, Jeanne Boyarsky and Scott Selikoff). All explanations and code examples here are my own, compiled and run on Ubuntu with JDK 21.

Normative references: Java Language Specification §7.3 (compilation units), §7.5 (import declarations), §12.1.4 (invoking main).