What does it mean by static main void in Java?

Sharjeel Siddique
3 min readMay 4, 2024

Ah, the age-old question that every beginner Java programmer has asked themselves at some point: “What in the world does public static void main(String[] args) mean?" You've been told to just write it and not ask questions, but that nagging curiosity remains. Well Today, we're going to demystify that cryptic line of code once and for all.

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

Imagine the Java Runtime Environment (JRE) as the stage manager, and your program is the star of the show. For the curtain to rise and the performance to begin, the JRE needs to call the main method of your program. It's like the stage manager shouting, "Places, everyone! Let's get this show on the road!"

To illustrate this, let’s consider a class called MainMethodExplanation. The JRE will essentially call the main method like this:

MainMethodExplanation.main(arguments);

Now, let’s break down the components of this method signature to understand their roles in the grand performance.

The Keyword Players

public

The public keyword is like your method waving its arms, trying to get the JRE's attention. If the method were private or protected, the JRE wouldn't have the necessary access to call it. It's like your method being stuck backstage, unable to take the spotlight.

static

“No need for an instance, JRE. Just call me directly!”

A static method can be called without creating an instance of the class. The JRE doesn't have to go through the trouble of creating an object of MainMethodExplanation and then calling the main method on that instance. It can just cut to the chase and run the main method directly on the class.

MainMethonExplanation callMe = new MainMethodExplanation(); 
// JRE doesn't need to create an object of MainMethoExplanation class

void

The void keyword indicates that the main method doesn’t need to return any value. It’s like your program taking a grand bow at the end of the performance — no need for an encore or an autograph session.

main

The JRE is hardwired to look for a method named main. You can't call it anything else, or the JRE will be scratching its head, wondering where the star of the show is.

This part is like giving your program a little extra flair or direction. The args parameter is an array of strings that can be passed as arguments when you run your program. It's like giving your program a script or a set of instructions to follow.

Let’s say you want your program to print a message that you provide as an argument. You can access that message using args[0]. Here's an example:

public static void main(String[] args) {System.out.println(args[0]);}

To run this program with the argument “Hello, World!”, you can use the command line or an IDE’s run configuration to pass in the arguments.

“Whoa, hold up! What if I don’t pass in any arguments?”

Good catch! If you don’t provide any arguments and try to access args[0], you'll get an ArrayIndexOutOfBoundsException. It's like your program trying to read a script that doesn't exist, resulting in a messy on-stage blunder.

The mystery of public static void main(String[] args) has been unveiled, and you're now equipped to take on Java programming with confidence. Whether you're writing a simple console application or a mind-bending, multi-threaded masterpiece, you'll always know where the show starts and how to give your program a standing ovation.

Now, if you are still not clear, feel free to leave a comment on this post. I’ll be more than happy to respond and provide further clarification to ensure that you have a solid understanding of all the stuff discussed.

--

--