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
Top comments (0)