DEV Community

Cover image for Your First Java Class — A Hello World Walkthrough
Md Jamilur Rahman
Md Jamilur Rahman

Posted on

Your First Java Class — A Hello World Walkthrough

You've installed Java. The terminal works. Now it's time to write something real — even if that something is the simplest program in existence.

Every developer remembers their first Hello World. It's not about the output. It's about proving the whole chain works: editor → compiler → JVM → result.

Let's walk through it.

What Is a Java Class?

Think of a class as a blueprint for a house. The blueprint describes what the house should look like — rooms, doors, windows. But the blueprint itself isn't a house. You can't live in it.

In Java, everything lives inside a class. Your code, your logic, your data — all packaged into one .java file. The JVM doesn't run loose functions. It needs a class to start with.

Here's the anatomy of even the smallest Java class:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
Enter fullscreen mode Exit fullscreen mode

Six lines. But each one carries weight. Let's break it down.

Line by Line

public class HelloWorld

This declares a class named HelloWorld. A few things to notice:

  • public — anyone can access this class from outside its file
  • class — the keyword that tells Java "this is a blueprint"
  • HelloWorld — the name. Java conventions say class names start with a capital letter and use PascalCase

There's one important rule: the filename must match the class name. If your class is called HelloWorld, the file must be HelloWorld.java. Get this wrong and the compiler will yell at you. No exceptions.

public static void main(String[] args)

This is the entry point — the door the JVM walks through to start your program. Without it, Java has no idea where to begin.

Let's unpack each word:

  • public — the JVM needs to access this method from outside the class
  • static — you can call this method without creating an object first. The JVM doesn't want to instantiate your class just to find the starting line
  • void — this method doesn't return anything. It just runs and finishes
  • main — the special name Java looks for. Name it anything else and it won't work
  • String[] args — an array of strings. These are command-line arguments you pass when running the program. We'll ignore them for now

This exact signature is non-negotiable. Change one word and your program won't start.

System.out.println("Hello, World!");

The actual work. This one line does three things:

  1. System — a built-in class that gives you access to standard I/O
  2. out — the "standard output stream" (your terminal)
  3. println — prints the text, then moves to a new line

The semicolon at the end is mandatory. Java uses semicolons to mark the end of a statement. Forget it and you'll see errors like ';' expected.

Writing, Compiling, Running

Here's the full workflow. Open your terminal and follow along.

Step 1: Create the file

mkdir hello-java
cd hello-java
Enter fullscreen mode Exit fullscreen mode

Create a file called HelloWorld.java with the code above. Use any text editor — nano, vim, VS Code, doesn't matter.

Step 2: Compile it

javac HelloWorld.java
Enter fullscreen mode Exit fullscreen mode

This runs the Java compiler (javac). It reads your .java file and produces a .class file — bytecode that the JVM understands.

If you see no output, that's good. Java compilers are quiet when they're happy. If you see errors, check:

  • Is the filename exactly HelloWorld.java?
  • Does the class name inside match exactly?
  • Is there a missing semicolon?

After successful compilation, run ls and you'll see HelloWorld.class alongside your source file.

Step 3: Run it

java HelloWorld
Enter fullscreen mode Exit fullscreen mode

Notice: no .class extension. The java command takes the class name, not the filename. The JVM loads HelloWorld.class, finds the main method, and runs it.

Output:

Hello, World!
Enter fullscreen mode Exit fullscreen mode

That's it. You just wrote, compiled, and ran your first Java program.

Common Beginner Mistakes

Wrong filename. Class is HelloWorld but file is helloWorld.java. Java is case-sensitive. This trips up almost everyone at least once.

Missing main method. Without public static void main(String[] args), the JVM throws Error: Main method not found. Your code compiles fine, but there's no door to enter.

Typo in System. Writing system (lowercase S) gives you cannot find symbol. Java is case-sensitive here too. It's always System with a capital S.

Forgetting the semicolon. The error message will point to the next line, which is confusing. Always check the line before the reported error.

What Just Happened Under the Hood?

When you ran javac HelloWorld.java, the compiler translated your human-readable code into bytecode — a platform-independent set of instructions stored in HelloWorld.class.

When you ran java HelloWorld, the JVM:

  1. Loaded the bytecode
  2. Verified it was safe
  3. Found the main method
  4. Executed your println statement

This two-step process (compile, then run) is why Java is called a compiled language with a virtual machine. The bytecode runs on any system with a JVM — Windows, macOS, Linux — without recompiling. Write once, run anywhere.

What's Next?

You've written a class, compiled it, and ran it. The chain works. Now you can start building on it:

  • Add more statements — try System.out.println with different text
  • Do mathSystem.out.println(2 + 2) prints 4
  • Try JShell — Java's interactive shell where you don't need a full class to test ideas

In the next article, we'll look at running a full Java application end to end — with multiple files, packages, and proper project structure.

Summary

  • Every Java program lives inside a class
  • The main method is the JVM's entry point — public static void main(String[] args)
  • Filename must match the class name exactly (case-sensitive)
  • javac compiles .java.class (bytecode)
  • java runs the bytecode via the JVM
  • Java is compiled → bytecode → virtual machine, which is why it's platform-independent

Based on dev.java/learn — Your First Java Class

Top comments (0)