DEV Community

Cover image for ๐Ÿ‘‰ The Java main Method: Why It Looks So Weird
Mohammed mhanna
Mohammed mhanna

Posted on

๐Ÿ‘‰ The Java main Method: Why It Looks So Weird

Introduction

Have you ever opened a fresh Java file and seen this line staring at you?

public static void main(String[] args) {
    // code here
}

Enter fullscreen mode Exit fullscreen mode

It looksโ€ฆ complicated.
Why all these words? Why not just main() like in other languages?

If youโ€™ve ever wondered why the Java main method looks so weird, youโ€™re not alone. In this post, Iโ€™ll break it down piece by piece so youโ€™ll never have to memorize it blindly again.


๐Ÿ“ฆ The Role of the main Method

The main method is the entry point of any Java program.
When you run your program, the Java Virtual Machine (JVM) looks for this exact method signature to start execution.

Without it, your code has no starting point.


๐Ÿ” Breaking Down the Weirdness

Letโ€™s decode each keyword in public static void main(String[] args) step by step:

  1. public โ€” Access from Anywhere

public means this method is visible to the JVM (and everything else).

If it werenโ€™t public, the JVM couldnโ€™t call it.
Think of it as leaving your front door open for the JVM to enter your program. ๐Ÿšช

  1. static โ€” No Objects Needed

static means the method belongs to the class, not an object.

The JVM can call it without creating an object first.
Imagine if you had to new an object every time you wanted to run your program โ€” painful!

  1. void โ€” No Return Value

void means the method doesnโ€™t return anything.

The JVM just needs a starting point to run your program โ€” it doesnโ€™t expect a result back.

  1. main โ€” The Name Matters

The JVM specifically looks for a method called main.
If you rename it to start or helloWorld, your program wonโ€™t run.

  1. String[] args โ€” Command-Line Data

This is how your program can accept input from the command line.

Example:

public class Main {
    public static void main(String[] args) {
        System.out.println("First argument: " + args[0]);
    }
}
Enter fullscreen mode Exit fullscreen mode

If you run:

java Main hello

Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ Output: First argument: hello


โšก Why It Looks So Weird (Compared to Other Languages)

In Python, you just write:

print("Hello, world!")

Enter fullscreen mode Exit fullscreen mode

In C, you write:

int main() {
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

But Java is object-oriented from the ground up.
Thatโ€™s why public static void main(String[] args) has to carry extra keywords โ€” to satisfy Javaโ€™s strict rules.


โœ… Quick Memory Trick

๐Ÿ‘‰ Public โ†’ Let the JVM in
๐Ÿ‘‰ Static โ†’ No object needed
๐Ÿ‘‰ Void โ†’ Donโ€™t return anything
๐Ÿ‘‰ Main โ†’ JVMโ€™s entry point
๐Ÿ‘‰ String[] args โ†’ Extra data from the outside world


๐Ÿ“š Further Resources

๐Ÿ”— Official Java Documentation

๐Ÿ”— (Geeks For Geeks): Java main() method


Conclusion

Yes, the Java main method looks weird at first.
But once you break it down, each keyword makes perfect sense.

Next time you see it, youโ€™ll know:

Why it has to be public

Why itโ€™s static

Why it returns nothing

And how it handles external data

๐Ÿ‘‰ Your turn: Whatโ€™s one Java keyword (final, static, super, etc.) that always confused you? Drop it in the comments โ€” maybe Iโ€™ll cover it in the next post!

Top comments (0)