DEV Community

Vigneshwaralingam
Vigneshwaralingam

Posted on

15 Common Java Mistakes in a Hello World Program (and How to Fix Them!)πŸš€

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

βœ… This program prints:

Hello, World!
Enter fullscreen mode Exit fullscreen mode

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

πŸ›  Fix: Use lowercase main

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

πŸ’‘ 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
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ›  Fix: Add a semicolon (;) at the end

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

πŸ’‘ 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
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ›  Fix: Use lowercase out.println

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

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

πŸ›  Fix: Add opening and closing braces {}

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

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

πŸ›  Fix: Use static void main

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

πŸ’‘ 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
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ›  Fix: Use double quotes

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

πŸ’‘ 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"
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ›  Fix: Use System.out.println

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

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

πŸ›  Fix: Change private to public

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

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

πŸ›  Fix: Add String[] args in the main method

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

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

πŸ›  Fix: Remove static from class declaration

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

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

πŸ›  Fix: Add public to class definition

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

πŸ’‘ 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
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ›  Fix: Remove unnecessary spaces

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

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

πŸ›  Fix: Replace void with String[]

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

πŸ’‘ 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"
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ›  Fix: Capitalize System

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

πŸ’‘ 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! πŸš€

Top comments (0)