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!");
}
}
✅ This program will print:
Hello, World!
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!");
}
}
🛠 Fix:
public class HelloWorld {
public static void main(String[] args) { // ✅ Use lowercase "main"
System.out.println("Hello, World!");
}
}
💡 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
}
}
🛠 Fix:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!"); // ✅ Add semicolon
}
}
💡 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
}
}
🛠 Fix:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!"); // ✅ Use "out" and "println" (lowercase)
}
}
💡 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!");
}
🛠 Fix:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
💡 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!");
}
}
🛠 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!");
}
}
🛠 Fix:
public class HelloWorld {
public static void main(String[] args) { // ✅ "void" should not be before "static"
System.out.println("Hello, World!");
}
}
💡 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!");
}
}
🛠 Fix:
public class HelloWorld { // ✅ Add "public"
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
💡 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
}
}
🛠 Fix:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!"); // ✅ Use double quotes
}
}
💡 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"
}
}
🛠 Fix:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!"); // ✅ Use "System.out.println"
}
}
💡 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