Skip to content

Lab: your first Java file

☕ Java 17 · How Java Works Under the Hood · 3/3

Goal: go through the full write → compile → run cycle with the first class of our Netflix-style streaming platform.

Setup: open an empty folder (for example netflix-platform) in your editor, open a terminal in it, and create a file named exactly Movie.java — capital M. No build tool needed.

Step 1 — Declare the class

public class Movie {

}
  • public makes the class visible to everything, including the JVM.
  • class says you are defining a blueprint, and Movie is its name.
  • The public class name must match the file name exactly: Movie lives in Movie.java.

Step 2 — Add two fields

Inside the braces:

    // Title of the movie on the platform
    String title;

    // Duration in minutes
    int durationMinutes;

String holds text, like "Inception". int holds a whole number, like 148. Lines starting with // are comments: notes for humans, ignored by the compiler.

Step 3 — Add a main method

To run the class, it needs a main method. Add it after the fields:

    public static void main(String[] args) {
        System.out.println("Welcome to the Netflix Platform!");
        System.out.println("Movie class loaded successfully.");
        System.out.println("Fields defined: title (String), durationMinutes (int)");
    }

main is static, so it can't use title directly yet: that needs an object, which comes a few sections later.

Step 4 — Compile

$ javac Movie.java

No output means no errors. A new file, Movie.class, appears next to Movie.java: that's the bytecode.

Step 5 — Run

$ java Movie
Welcome to the Netflix Platform!
Movie class loaded successfully.
Fields defined: title (String), durationMinutes (int)

The JVM loaded Movie.class, found main and ran its lines from top to bottom.

Final code

public class Movie {

    // Title of the movie on the platform
    String title;

    // Duration in minutes
    int durationMinutes;

    public static void main(String[] args) {
        System.out.println("Welcome to the Netflix Platform!");
        System.out.println("Movie class loaded successfully.");
        System.out.println("Fields defined: title (String), durationMinutes (int)");
    }
}

Full code on GitHub

← Your JDK · Back to the series


🎓 Preparing the OCP Java 17 exam? Exam traps and practice questions for this section → (optional)