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)