DEV Community

Vigneshwaralingam
Vigneshwaralingam

Posted on

Common Java Errors in a Hello World Program and How to Fix Them.

Common Java Errors in a Hello World Program and How to Fix Them

In this blog, we will:

✅ Introduce a basic Hello World program in Java.

✅ Identify 15 common errors that beginners make.

✅ Explain why these errors happen and how to fix them.


📌 Correct Hello World Program in Java

Before we explore errors, let's look at a correct Hello World program:

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

This program will print:

Hello, World!
Enter fullscreen mode Exit fullscreen mode

Now, let's introduce 15 common errors and their fixes.


🔍 15 Common Errors in Java and Their Solutions

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!");
    }
}
Enter fullscreen mode Exit fullscreen mode

🛠 Fix:

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

💡 Why? Java is case-sensitive. The JVM looks for main, not Main.


2️⃣ Error: Missing Semicolon (;)

🔴 Wrong Code:

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

🛠 Fix:

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

💡 Why? Java statements must end with a semicolon.


3️⃣ Error: Incorrect 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
    }
}
Enter fullscreen mode Exit fullscreen mode

🛠 Fix:

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

💡 Why? System.out.println must be 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!");
    }
Enter fullscreen mode Exit fullscreen mode

🛠 Fix:

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

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


5️⃣ Error: Class Name and Filename Mismatch

🔴 Wrong Code: (File is 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!");
    }
}
Enter fullscreen mode Exit fullscreen mode

🛠 Fix:

  • Either rename the file to HelloWorld.java, or
  • Change the class name to Hello

💡 Why? The filename must match the class name when using public class.


6️⃣ Error: Using void in the Wrong Place

🔴 Wrong Code:

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

🛠 Fix:

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

💡 Why? static comes before void in the main method declaration.


7️⃣ Error: Missing public Keyword for the Class

🔴 Wrong Code:

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

🛠 Fix:

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

💡 Why? Java expects the main class to be public if it's the main entry point.


8️⃣ 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
    }
}
Enter fullscreen mode Exit fullscreen mode

🛠 Fix:

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

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


9️⃣ Error: Using println without System.out

🔴 Wrong Code:

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

🛠 Fix:

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

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


🔍 Summary of Common Errors

Error Type Mistake Fix
Syntax Error Misspelled main Use main, not Main
Syntax Error Missing semicolon Add ; at the end
Syntax Error Wrong System.out.println Use lowercase out.println
Compilation Error Class and filename mismatch Rename the file or class
Compilation Error Missing public keyword Add public before class
Logical Error Wrong string quotes Use "" instead of ''

🎯 Final Thoughts

Errors are a part of learning Java programming. The key to mastering coding is:

✅ Reading error messages carefully

✅ Practicing regularly

✅ Using an IDE (like IntelliJ or VS Code) for auto-suggestions

Debugging step by step


❓ What was the most frustrating error you faced in Java? Let me know in the comments!

Top comments (0)