Skip to content

OCP exam prep: class structure

☕ Java 17 · 🎓 OCP 1Z0-829 · Your First Java Class

Optional page

This page is for readers preparing the Oracle Certified Professional Java SE 17 Developer exam (1Z0-829). The concepts are all you need otherwise.

Exam objective: Create classes, and define and use instance and static fields and methods.

Four traps to watch for

Trap 1 — The main signature must be exact

public static void main(String[] args) (or String... args). Without static, with int instead of void, or with String instead of String[], the JVM can't use it as an entry point.

Trap 2 — File name = public class name

A public class Movie must be in Movie.java, letter case included. A mismatch is a compiler error, not a runtime error.

Trap 3 — Code hidden in comments

Questions often comment out lines that look like code. Before tracing anything, check what // and /* */ remove.

Trap 4 — args starts at index 0

args[0] is the first argument. Reading it when no argument was passed throws ArrayIndexOutOfBoundsException at runtime.

Practice questions

1. Which signature lets the JVM run the program?

A. public static void main(String[] args)
B. public void main(String[] args)
C. static void main(String args)
D. public static int main(String[] args)

Answer

A. B misses static: the JVM doesn't create an object to call main. C takes a String, not a String[]. D returns int, but main must return void.

2. Which statement is true about this file?

// File: Content.java
public class Series {
    String title;
}
class Content { }

A. It compiles
B. It fails: a file can hold only one class
C. It fails: the file name doesn't match the public class
D. It fails: Content is not public

Answer

C. The public class is Series, so the file must be Series.java. Two classes in one file are fine as long as only one is public, and non-public classes don't need public.

← Back to the series · All 1Z0-829 pages