15 Common Java Errors in a Hello World Program (And How to Fix Them!)

Writing a simple Hello World program in Java might seem easy, but beginners often run into common errors. These mistakes can be frustrating, but understanding why they happen and how to fix them will make you a better programmer.

In this blog, we'll cover:

✅ A correct Hello World program in Java.

15 common errors beginners make.

✅ How to fix each error and understand why they happen.


📌 Correct Hello World Program in Java

Before we explore the errors, let’s first see what a correct Hello World program looks like:

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

This program prints:

Hello, World!

Now, let's check out 15 common errors and how to fix them.


🔍 15 Common Java Errors (With Fixes!)

1️⃣ Error: Misspelled main Method

🔴 Wrong Code:

public class HelloWorld {
    public static void Main(String[] args) { // ❌ "Main" should be "main"
        System.out.println("Hello, World!");
    }
}

🛠 Fix: Use lowercase main

public class HelloWorld {
    public static void main(String[] args) { // ✅ Correct
        System.out.println("Hello, World!");
    }
}

💡 Why? Java is case-sensitive, and main must be lowercase.


2️⃣ Error: Missing Semicolon (;)

🔴 Wrong Code:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!") // ❌ Missing semicolon
    }
}

🛠 Fix: Add a semicolon (;) at the end

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); // ✅ Correct
    }
}

💡 Why? Every Java statement must end with a semicolon.


3️⃣ Error: Wrong System.out.println Syntax

🔴 Wrong Code:

public class HelloWorld {
    public static void main(String[] args) {
        System.Out.Println("Hello, World!"); // ❌ "Out" and "Println" are incorrect
    }
}

🛠 Fix: Use lowercase out.println

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); // ✅ Correct
    }
}

💡 Why? System.out.println must have lowercase out and println.


4️⃣ Error: Missing Curly Braces ({})

🔴 Wrong Code:

public class HelloWorld 
    public static void main(String[] args) { // ❌ Missing opening brace `{`
        System.out.println("Hello, World!");
    }

🛠 Fix: Add opening and closing braces {}

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

💡 Why? Every class and method must have curly braces {} to define their scope.


5️⃣ Error: Class Name and Filename Mismatch

🔴 Wrong Code: (File saved as Hello.java)

public class HelloWorld { // ❌ Class name "HelloWorld" does not match filename "Hello.java"
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

🛠 Fix: Either rename the file to HelloWorld.java or change the class name to Hello.

💡 Why? Java expects the filename to match the class name if the class is public.


6️⃣ Error: Incorrect void Placement

🔴 Wrong Code:

public class HelloWorld {
    public void static main(String[] args) { // ❌ "void" should not be before "static"
        System.out.println("Hello, World!");
    }
}

🛠 Fix: Use static void main

public class HelloWorld {
    public static void main(String[] args) { // ✅ Correct order
        System.out.println("Hello, World!");
    }
}

💡 Why? The correct order is public static void main.


7️⃣ Error: Using Single Quotes Instead of Double Quotes

🔴 Wrong Code:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println('Hello, World!'); // ❌ Uses single quotes
    }
}

🛠 Fix: Use double quotes

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); // ✅ Correct
    }
}

💡 Why? Strings in Java must be inside double quotes (""), not single quotes ('').


8️⃣ Error: Using println Without System.out

🔴 Wrong Code:

public class HelloWorld {
    public static void main(String[] args) {
        println("Hello, World!"); // ❌ Missing "System.out"
    }
}

🛠 Fix: Use System.out.println

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); // ✅ Correct
    }
}

💡 Why? println is a method of System.out, so it must be used with it.


9️⃣ Error: Declaring main as Private

🔴 Wrong Code:

public class HelloWorld {
    private static void main(String[] args) { // ❌ "main" should be public
        System.out.println("Hello, World!");
    }
}

🛠 Fix: Change private to public

public class HelloWorld {
    public static void main(String[] args) { // ✅ Correct
        System.out.println("Hello, World!");
    }
}

💡 Why? The main method must be public, so the JVM can execute it.

🔟 Error: Forgetting String[] args in the Main Method

🔴 Wrong Code:

public class HelloWorld {
    public static void main() { // ❌ Missing "String[] args"
        System.out.println("Hello, World!");
    }
}

🛠 Fix: Add String[] args in the main method

public class HelloWorld {
    public static void main(String[] args) { // ✅ Correct
        System.out.println("Hello, World!");
    }
}

💡 Why? The Java Virtual Machine (JVM) expects main(String[] args), so removing String[] args causes an error.


1️⃣1️⃣ Error: Declaring Class as public static

🔴 Wrong Code:

public static class HelloWorld { // ❌ "static" should not be here
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

🛠 Fix: Remove static from class declaration

public class HelloWorld { // ✅ Correct
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

💡 Why? In Java, top-level classes cannot be static.


1️⃣2️⃣ Error: Missing public in Class Declaration

🔴 Wrong Code: (When filename is HelloWorld.java)

class HelloWorld { // ❌ Should be "public class"
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

🛠 Fix: Add public to class definition

public class HelloWorld { // ✅ Correct
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

💡 Why? If the filename is HelloWorld.java, the class must be public.


1️⃣3️⃣ Error: Extra Spaces in System.out.println

🔴 Wrong Code:

public class HelloWorld {
    public static void main(String[] args) {
        System . out . println ( "Hello, World!" ); // ❌ Extra spaces
    }
}

🛠 Fix: Remove unnecessary spaces

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); // ✅ Correct
    }
}

💡 Why? While Java allows spaces between tokens, too many spaces make code unreadable and error-prone.


1️⃣4️⃣ Error: Using void Instead of String[] in Main Method

🔴 Wrong Code:

public class HelloWorld {
    public static void main(void args) { // ❌ "void" is incorrect
        System.out.println("Hello, World!");
    }
}

🛠 Fix: Replace void with String[]

public class HelloWorld {
    public static void main(String[] args) { // ✅ Correct
        System.out.println("Hello, World!");
    }
}

💡 Why? The main method must take String[] args as an argument, not void.


1️⃣5️⃣ Error: Using system.out.println Instead of System.out.println

🔴 Wrong Code:

public class HelloWorld {
    public static void main(String[] args) {
        system.out.println("Hello, World!"); // ❌ "system" should be "System"
    }
}

🛠 Fix: Capitalize System

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); // ✅ Correct
    }
}

💡 Why? System is a built-in Java class and must start with an uppercase S.


🎯 Final Thoughts

Understanding and fixing these 15 common errors will make you a better Java programmer. Debugging can be frustrating, but with practice, you'll learn to spot errors quickly and fix them easily.

🔥 Next Steps? Try writing your own Java programs and see if you can avoid these mistakes!

💬 Have you encountered any other Java errors? Let me know in the comments! 🚀