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!");
}
}
β
This program prints:
Hello, World!
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!");
}
}
π Fix: Use lowercase main
public class HelloWorld {
public static void main(String[] args) { // β
Correct
System.out.println("Hello, World!");
}
}
π‘ 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
}
}
π Fix: Add a semicolon (;) at the end
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!"); // β
Correct
}
}
π‘ 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
}
}
π Fix: Use lowercase out.println
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!"); // β
Correct
}
}
π‘ 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!");
}
π Fix: Add opening and closing braces {}
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
π‘ 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!");
}
}
π 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!");
}
}
π Fix: Use static void main
public class HelloWorld {
public static void main(String[] args) { // β
Correct order
System.out.println("Hello, World!");
}
}
π‘ 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
}
}
π Fix: Use double quotes
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!"); // β
Correct
}
}
π‘ 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"
}
}
π Fix: Use System.out.println
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!"); // β
Correct
}
}
π‘ 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!");
}
}
π Fix: Change private to public
public class HelloWorld {
public static void main(String[] args) { // β
Correct
System.out.println("Hello, World!");
}
}
π‘ 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!");
}
}
π Fix: Add String[] args in the main method
public class HelloWorld {
public static void main(String[] args) { // β
Correct
System.out.println("Hello, World!");
}
}
π‘ 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!");
}
}
π Fix: Remove static from class declaration
public class HelloWorld { // β
Correct
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
π‘ 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!");
}
}
π Fix: Add public to class definition
public class HelloWorld { // β
Correct
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
π‘ 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
}
}
π Fix: Remove unnecessary spaces
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!"); // β
Correct
}
}
π‘ 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!");
}
}
π Fix: Replace void with String[]
public class HelloWorld {
public static void main(String[] args) { // β
Correct
System.out.println("Hello, World!");
}
}
π‘ 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"
}
}
π Fix: Capitalize System
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!"); // β
Correct
}
}
π‘ 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)