Java Building Blocks: JDK, JVM, Class Structure and the main Method (OCP 21)
The OCP Java 21 basics: what the JDK, javac, java and the JVM do, how a class and a source file are structured, the three comment styles, every legal form of main(), command-line arguments, single-file launching and Java 25's compact source files.
kvi5hnuCode Sprout team member11 min readOn this page
Every exam question assumes you can read a Java source file the way the compiler does: which parts are comments, which class is public, whether main can be launched. This article covers that foundation: the tools in the JDK, the parts of a class, how classes relate to files, and the rules for the main() method that starts a program. Several of these rules changed after Java 21, so watch the callouts if you're aiming for the Java 25 exam.
The JDK and the JVM
The Java Development Kit (JDK) is the minimum software you need to write Java programs. Four of its commands matter here:
| Command | What it does |
|---|---|
javac | Compiles .java source files into .class files containing bytecode |
java | Starts a Java Virtual Machine (JVM) that loads and runs bytecode |
jar | Packages files, usually class files and resources, into a .jar archive |
javadoc | Generates HTML documentation from special comments |
Bytecode is not machine code for any particular processor. It's an instruction set for the JVM, and every platform has its own JVM that knows how to run bytecode on that operating system and hardware. That's why a class file compiled on Windows runs unchanged on Linux or macOS.

Versions and distributions
A new Java version is released every six months, in March and September. Every two years one release is a long-term support (LTS) release: Java 17 (2021), 21 (2023) and 25 (2025). Java 21 was released in September 2023, and it is the version the 1Z0-830 exam tests.
Oracle publishes its own JDK, and many vendors publish builds of the open-source OpenJDK code, such as Eclipse Temurin, Amazon Corretto and Microsoft's build. For the exam they are all the same Java.
Check which version is on your path with javac -version and java -version (java --version with two dashes also works and prints to standard output instead of standard error).
Preview features are finished-but-not-final features that a release ships switched off. You have to opt in with --enable-preview. They are never on the exam. When this course says a feature was "added in Java N", it means the version where the feature became final.
The parts of a class
Classes are the basic building blocks of Java programs. An object is a runtime instance of a class, and a reference is a variable that points to an object.
A class has two kinds of members: fields (variables that hold state) and methods (operations on that state).
public class Animal { // class declaration
String name; // field
public String getName() { // method returning a String
return name;
}
public void setName(String newName) { // method returning nothing (void)
name = newName;
}
}
A few terms the exam uses precisely:
- A keyword is a word with special meaning to the compiler, such as
public,classorvoid. - A parameter is a variable in a method declaration, such as
newName. - The method signature is the method's name plus its parameter types:
setName(String). The return type and the parameter names are not part of the signature. This matters later for overloading and overriding. voidis a return type meaning "returns no value".
Comments
Java has three kinds of comments:
// single-line comment: runs to the end of the line
/* multiline comment:
runs until the first closing star-slash */
/**
* Javadoc comment: a multiline comment that starts with slash-star-star.
* The javadoc tool turns these into documentation.
*/
The exam won't ask you to write Javadoc, but it uses comments to hide traps. Two rules settle every case:
- Everything after
//up to the end of the line is a comment, even text that looks like/*or*/. - A multiline comment ends at the first
*/. Multiline comments don't nest.
So /* outer /* inner */ */ doesn't compile. The comment ends after inner, and the compiler then sees a stray */.
/* // this is all one block comment */
// /* this is a line comment, not the start of a block */
/* a single-line block comment is still a block comment */
Classes and source files
A top-level type is a type declared directly in a source file, not inside another type. In this chapter the only top-level type is a class. Interfaces, enums and records come later.
The rules connecting classes to files:
- A file can contain any number of top-level types, but at most one of them can be
public. - If a top-level type is
public, the file must be named after it:public class Animalmust be inAnimal.java, with the same capitalization. - A non-public top-level class can live in a file with any name.
// File: Animal.java
public class Animal { } // OK: public and matches the file name
class Helper { } // OK: not public
// public class Other { } // DOES NOT COMPILE: second public class, wrong file name
The main() method
A Java program starts at a main() method, the entry point that the JVM calls. The classic form is:
public class Trip {
public static void main(String[] args) {
System.out.println("Hello, traveller");
}
}
Compile and run it with:
javac Trip.java
java Trip
Note the difference: javac takes a file name with the .java extension, while java takes a class name without .class. Typing java Trip.class fails with a "could not find or load main class" error.
Each word of the declaration has a job:
public: the JVM, which lives outside your class, must be able to call it.static: the JVM calls it without creating an object, asTrip.main(...).void: it returns nothing. A program ends whenmain(and any other non-daemon threads) finish.String[] args: the command-line arguments as an array of strings.
Legal variations
The compiler and launcher accept several spellings of the same entry point:
public static void main(String[] args) // the usual form
static public void main(String[] args) // modifiers in either order
public static void main(String args[]) // C-style array brackets
public static void main(String... args) // varargs
public static void main(String[] whatever) // any parameter name
public final static void main(final String[] a) // optional final modifiers
final is optional on both the method and the parameter. synchronized and strictfp are also allowed, but they don't appear on the exam.
What is not an entry point (Java 21)
These all compile, because they're ordinary methods, but Java 21 can't use them to start a program. It reports that no valid main method was found:
public void main(String[] args) // not static
static void main(String[] args) // not public
public static int main(String[] args) // wrong return type
public static void main(String args) // not an array
public static void Main(String[] args) // name is case sensitive
A class can have any number of methods named main with other parameter lists. They're just overloads, and only the String[] one is the entry point.
Passing arguments
Arguments after the class name are split on spaces and passed in args:
java Trip Paris Rome → args = ["Paris", "Rome"]
java Trip "New Delhi" Rome → args = ["New Delhi", "Rome"]
java Trip → args = [] (an empty array, never null)
Quotes keep a space inside one argument, and the quotes themselves are removed. Array indexes start at 0, so reading args[1] when only one argument was passed throws ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 at runtime. It's a runtime failure, not a compile error.
All arguments arrive as Strings. java Calc 1 2 gives you "1" and "2", which you convert with Integer.parseInt if you need numbers.
Single-file source-code programs
Since Java 11 you can skip the separate compile step for a program contained in one file:
java Trip.java Paris Rome
Notice that this time the .java extension is included. The launcher compiles the file in memory and runs it. Facts worth knowing:
- No
.classfile is written to disk. - The class that runs is the first top-level class in the file, and it must declare a valid
mainmethod. - Everything after the file name is passed to
mainas arguments. - In Java 21 the program must fit in one source file. Java 22 (JEP 458) lets the launched file use other source files in the same directory tree.
The mode is meant for small programs and experiments. It's perfect for studying for this exam.
What's current
- Java 25 finalized compact source files and instance main methods (JEP 512), so
void main()with no class declaration can be launched. 1Z0-830 still expectspublic static void main(String[] args). - Java 22 lets
java Main.javacompile and run a program spread over several source files (JEP 458). Java 11's single-file launch is still current.
Exam traps from this article
A complete example
main is just a static method, so a program can call another class's main directly and pass whatever array it likes. This program uses that to simulate two runs of Trip with different arguments. It also shows a non-public class sharing a file with the public one, and comment markers that don't do what they look like.
public class Solution {
public static void main(String[] args) {
System.out.println("Solution received " + args.length + " arguments");
// main is an ordinary static method, so we can call it ourselves
Trip.main(new String[] {"New Delhi", "Rome"});
Trip.main(new String[] {"Paris"});
/* A block comment may contain // without ending early */
int bags = 3; // a line comment may contain /* without starting a block
System.out.println("bags = " + bags);
}
}
class Trip { // not public, so its name needn't match the file
public static void main(String... stops) { // varargs form of the main parameter
System.out.println("first: " + stops[0]);
if (stops.length > 1) {
System.out.println("second: " + stops[1]);
} else {
System.out.println("no second argument");
}
}
}
Solution received 0 arguments
first: New Delhi
second: Rome
first: Paris
no second argument
bags = 3
Run it locally with java Solution.java one two and the first line reports two arguments. Compile it with javac Solution.java instead, and you get two class files, Solution.class and Trip.class. The compiler writes one class file per class, not per source file.
Common questions
Do I need the JDK or is a "Java runtime" enough to study?
You need the JDK, because it contains javac. Since Java 11 there's no separate JRE download from Oracle anyway. Install a current JDK and use --release 21 when you want the compiler to enforce Java 21 rules exactly.
Why does java Trip say it can't find the class when Trip.class is right there?
Usually the class is in a package. A class declared in package travel; must be run as java travel.Trip from the directory above travel/. The next article covers packages and the class path.
Is String... args really the same as String[] args?
For main, yes. Inside the method, a varargs parameter is an array. The difference only matters to callers of an ordinary method, who can then pass the strings one by one instead of building an array.
Can a class have two main methods?
It can have several methods named main with different parameter lists (overloads). Only one of them can be public static void main(String[]), because two methods with the same signature in one class don't compile.
Does Java 21 support void main() without a class?
Only as a preview feature, with --enable-preview. It became final in Java 25, so it's fair game on the Java 25 exam but not on 1Z0-830.
Summary
- The JDK provides
javac(source to bytecode),java(runs bytecode on a JVM),jarandjavadoc. Bytecode is portable because each platform has its own JVM. - A class contains fields and methods. A method signature is the name plus the parameter types.
- Comments come in three kinds:
//,/* */and/** */. A block comment ends at the first*/and can't be nested. - A file may hold many top-level types, but at most one can be
public, and it must match the file name. - The Java 21 entry point is
public static void main(String[] args), in any of its legal spellings: modifiers in either order,String args[],String... args, any parameter name, optionalfinal. - Arguments are split on spaces, quotes group words, and
argsis empty (notnull) when none are passed. java File.javacompiles and runs a single source file in memory, and Java 25 adds compact source files and instancemainmethods.
Practice quiz
Test yourself with the JDK, class structure and main() quiz: 10 exam-style questions with explanations.
Previous: OCP Java 21 Exam Guide (1Z0-830): Format, Objectives, Strategy and Study Plan
Next: Java Packages and Imports: Wildcards, Conflicts, Compiling and the Class Path (OCP 21)
Related reading
- Writing a Class Loader and Bytecode Verification in Java
- Java Bytecode Engineering: Modifying Class Files with ASM and Java Agents
- OCP Java 21 Exam Guide (1Z0-830): Format, Objectives, Strategy and Study Plan
- Swing Choice Components: Check Boxes, Radio Buttons, Borders, Combo Boxes and Sliders
- Class Loading in Java: The Loading Process, Loader Hierarchy and Namespaces

More in OCP Java 21
OCP Java 21 Mock Exam 3 (1Z0-830)
The third full 50-question mock exam for the OCP Java SE 21 Developer exam (1Z0-830), weighted toward the hardest areas: streams, concurrency, generics, modules and I/O. 120 minutes, 68% to pass, with explanations.
OCP Java 21 Mock Exam 2 (1Z0-830)
The second full 50-question mock exam for the OCP Java SE 21 Developer exam (1Z0-830): new questions on every objective, with more multi-select. 120 minutes, 68% to pass, with an explanation for every answer.
OCP Java 21 Mock Exam 1 (1Z0-830)
A full 50-question mock exam for the OCP Java SE 21 Developer exam (1Z0-830), covering every objective at exam difficulty. 120 minutes, 68% to pass, with an explanation for every answer.
Quiz: Final Review (OCP 21)
Ten exam-style questions mixing the most-tested OCP Java 21 rules from the final checklist: promotion, String immutability, overloading, initialization order, collections, streams, exceptions and switch patterns, with explanations.
Discussion
Sign in to ask a question or share what you learned.