Here is a little bit detailed explanation on why main method is declared as

public static void main(String[] args)

Main method is the entry point of a Java program for the Java Virtual Machine(JVM). Let's say we have a class called Sample

class Sample {
     static void fun()
     {
           System.out.println("Hello");       
     }  
}

class Test {
     public static void main(String[] args)
     {
           Sample.fun();
     }  
}

This program will be executed after compilation as java Test. The java command will start the JVM and it will load our Test.java class into the memory. As main is the entry point for our program, JVM will search for main method which is declared as public, static and void.

Why main must be declared public?

main() must be declared public because as we know it is invoked by JVM whenever the program execution starts and JVM does not belong to our program package.

In order to access main outside the package we have to declare it as public. If we declare it as anything other than public it shows a Run time Error but not Compilation time error.

Why main must be declared static?

main() must be declared as static because JVM does not know how to create an object of a class, so it needs a standard way to access the main method which is possible by declaring main() as static.

If a method is declared as static then we can call that method outside the class without creating an object using the syntax ClassName.methodName();.

So in this way JVM can call our main method as <ClassName>.<Main-Method>

Why main must be declared void?

main() must be declared void because JVM is not expecting any value from main(). So, it must be declared as void.

If any other return type is provided, then it is a RunTimeError i.e., NoSuchMethodFoundError.

Why main must have String Array Arguments?

main() must have String arguments as arrays because JVM calls main method by passing command line argument. As they are stored in String array object it is passed as an argument to main().

Answer from Sai Upadhyayula on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-main-method-public-static-void-main-string-args
Java main() Method - public static void main(String[] args) - GeeksforGeeks
August 17, 2026 - If we change the name while initiating main method, we will get an error. ... It stores Java command-line arguments and is an array of type java.lang.String class. Here, the name of the String array is args but it is not fixed and the user can use any name in place of it. ... class Geeks { public static void main(String[] args) { for (String elem : args) System.out.println(elem); } }
🌐
DigitalOcean
digitalocean.com › community › tutorials › public-static-void-main-string-args-java-main-method
Understanding public static void main(String[] args) in Java | DigitalOcean
June 26, 2025 - Learn what public static void main(String[] args) means in Java. Understand each keyword’s purpose and how the main method powers Java programs.
Discussions

java - What does `public static void main args` mean? - Stack Overflow
I am not sure what this means, whenever before you write a code, people say this public static void main(String[] args) { What does that mean? More on stackoverflow.com
🌐 stackoverflow.com
java - Explanation of 'String args[]' and static in 'public static void main(String[] args)' - Stack Overflow
These are the arguments of type String that your Java application accepts when you run it. ... I would point a beginner to the Wiki article on the Main function, then supplement it with this. Java only starts running a program with the specific public static void main(String[] args) signature, ... More on stackoverflow.com
🌐 stackoverflow.com
public static void main(String arg[ ] ) in java is it fixed? - Stack Overflow
I was recently asked in an exam if public static void main(String arg[]) format of main method was fixed? Can we change it? Can we use main without any of public, static or void? If not, why is it ... More on stackoverflow.com
🌐 stackoverflow.com
The death of the public static void main(String[] args) meme
I mean…the ridicule doesn’t really bother us does it? Sure Java is verbose but that’s why it’s a pleasure to work with. It’s readable and it spells out exactly what it’s doing. More on reddit.com
🌐 r/java
63
93
February 27, 2024
🌐
Reddit
reddit.com › r/learnprogramming › what is "public static void main(string[] args) {" ?
r/learnprogramming on Reddit: What is "public static void main(String[] args) {" ?
February 20, 2017 -

Why is that used in every program, what is it called, and what is it used for? And do you need "public class insertname" at the top line in every program? I'm using Eclipse and im trying to learn Java, thanks.

Top answer
1 of 7
23

Here is a little bit detailed explanation on why main method is declared as

public static void main(String[] args)

Main method is the entry point of a Java program for the Java Virtual Machine(JVM). Let's say we have a class called Sample

class Sample {
     static void fun()
     {
           System.out.println("Hello");       
     }  
}

class Test {
     public static void main(String[] args)
     {
           Sample.fun();
     }  
}

This program will be executed after compilation as java Test. The java command will start the JVM and it will load our Test.java class into the memory. As main is the entry point for our program, JVM will search for main method which is declared as public, static and void.

Why main must be declared public?

main() must be declared public because as we know it is invoked by JVM whenever the program execution starts and JVM does not belong to our program package.

In order to access main outside the package we have to declare it as public. If we declare it as anything other than public it shows a Run time Error but not Compilation time error.

Why main must be declared static?

main() must be declared as static because JVM does not know how to create an object of a class, so it needs a standard way to access the main method which is possible by declaring main() as static.

If a method is declared as static then we can call that method outside the class without creating an object using the syntax ClassName.methodName();.

So in this way JVM can call our main method as <ClassName>.<Main-Method>

Why main must be declared void?

main() must be declared void because JVM is not expecting any value from main(). So, it must be declared as void.

If any other return type is provided, then it is a RunTimeError i.e., NoSuchMethodFoundError.

Why main must have String Array Arguments?

main() must have String arguments as arrays because JVM calls main method by passing command line argument. As they are stored in String array object it is passed as an argument to main().

2 of 7
1

According to the Java language specification, a Java program's execution starts from main() method. A main() method should follow the specific syntax, it can be explained as:

public static void main(String[] args)

public - Access specifier, shows that main() is accessible to all other classes.

void - return type, main() returns nothing.

String args[] - arguments to the main() method, which should an array of type string.

static - Access modifier. A main method should always be static, because the `main()' method can be called without creating an instance of the class.

Let us assume, we are executing a Helloworld java program.

While executing the program, we use the command

java Helloworld.

Internally, this command is converted into

Helloworld.main()

By making main() method static, JVM calls the main() method without creating an object first.

Top answer
1 of 8
97

I would break up

public static void main(String args[])

in parts:

public

It means that you can call this method from outside of the class you are currently in. This is necessary because this method is being called by the Java runtime system which is not located in your current class.


static

When the JVM makes call to the main method there is no object existing for the class being called therefore it has to have static method to allow invocation from class.


void

Java is platform independent language and if it will return some value then the value may mean different things to different platforms. Also there are other ways to exit the program on a multithreaded system. Detailed explaination.


main

It's just the name of method. This name is fixed and as it's called by the JVM as entry point for an application.


String args[]

These are the arguments of type String that your Java application accepts when you run it.

2 of 8
34

I would point a beginner to the Wiki article on the Main function, then supplement it with this.

  • Java only starts running a program with the specific public static void main(String[] args) signature, and one can think of a signature like their own name - it's how Java can tell the difference between someone else's main() and the one true main().

  • String[] args is a collection of Strings, separated by a space, which can be typed into the program on the terminal. More times than not, the beginner isn't going to use this variable, but it's always there just in case.

🌐
W3Schools
w3schools.com › java › java_methods.asp
Java Methods
public class Main { static void myMethod() { System.out.println("I just got executed!"); } public static void main(String[] args) { myMethod(); myMethod(); myMethod(); } } // I just got executed! // I just got executed! // I just got executed! Try it Yourself » ·
🌐
Java2Blog
java2blog.com › home › core java › java basics › public static void main(string[] args) – java main method
public static void main(String[] args) – Java main method - Java2Blog
June 11, 2021 - Yes, you can have multiple main method in the class, but when you execute java program, only method with signature public static void main(String[] args) will be called.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › understanding-static-in-public-static-void-main-in-java
Understanding "static" in "public static void main" in Java - GeeksforGeeks
July 11, 2025 - What if we don't write "static" before the main method: If we do not write "static" before the main method then, our program will be compiled without any compilation error(s). But at the time of execution, the JVM searches for the main method which is public, static, with a return type and a String array as an argument. If such a method is not found then an error is generated at the run time. ... /*package whatever //do not write package name here */ import java.io.*; class GFG { public void main (String[] args) { System.out.println("GFG!"); } }
🌐
Medium
medium.com › towardsdev › understanding-public-static-void-main-string-args-in-java-5d5666d4f1eb
Understanding public static void main(String[] args) in Java | by Nakul Mitra | Towards Dev
February 17, 2025 - Each argument is stored as a string in the array. ... public class Main { public static void main(String[] args) { System.out.println("Inside main method!!!"); for(String str : args){ System.out.println(str); } System.out.println("End of main ...
🌐
Medium
pratikraj.medium.com › string-args-in-java-c-main-string-args-fd9792e9753d
String args in Java , C# →main(String[] args) | by Pratik Raj | Medium
February 24, 2020 - String args in Java , C# →main(String[] args) Your class would still compile, but here main is treated as just another method, for eg public static void main() { Calendar calendar = …
🌐
TutorialsPoint
tutorialspoint.com › article › java-public-static-void-main-string-args
Java public static void main(String[] args)
June 19, 2025 - public class Main { public static void main( String[] args ) { double x = 6.55; double y = 4.32; System.out.println(" Ceil value of x: " + Math.ceil(x) ); System.out.println(" Floor value of y: " + Math.floor(y) ); } }
🌐
Medium
medium.com › @jasun.mckarthy93 › understanding-the-public-static-void-main-string-args-java-main-method-40c3cf8ffc99
Understanding the public static void main(String[] args) Java Main Method | by Jasun Mckarthy | Medium
September 18, 2024 - It would require extra code and could introduce complications, especially if the class had dependencies or constructors that needed specific arguments. The static keyword simplifies this by allowing the JVM to directly call the main method. ... class StaticExample { public static void main(String[] args) { System.out.println("This method is static and belongs to the class."); } }
🌐
W3Schools
w3schools.com › java › java_class_methods.asp
Java Class Methods
class Second { public static void main(String[] args) { Main myCar = new Main(); // Create a myCar object myCar.fullThrottle(); // Call the fullThrottle() method myCar.speed(200); // Call the speed() method } }
🌐
YouTube
youtube.com › watch
Java For Beginners - public static void main(String[] args) Detailed Explanation - YouTube
Next Video: http://youtu.be/hC32Gztjm3sPrevious Video: http://youtu.be/eD4cb3RR7NQVideo Description:In this video we will our very first java program and we'
Published: March 7, 2015
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › csharp › fundamentals › program-structure › main-command-line
Main() and command-line arguments - C# | Microsoft Learn
Unlike C and C++, the name of the program isn't treated as the first command-line argument in the args array, but it's the first element of the GetCommandLineArgs() method. The following list shows permutations of Main declarations: static void Main() { } static int Main() { } static void Main(string[] args) { } static int Main(string[] args) { } static async Task Main() { } static async Task<int> Main() { } static async Task Main(string[] args) { } static async Task<int> Main(string[] args) { }
🌐
Medium
automatethis.medium.com › chapter-4-understanding-public-static-void-main-string-args-in-java-a-simple-explanation-e45e129c84f5
Chapter[4]: Understanding public static void main(String[] args) in Java: A Simple Explanation | by Automate This. By Mrigank Saxena | Medium
November 24, 2024 - public class MainExample { public static void main(String[] args) { // Print a welcome message System.out.println("Welcome to the Java Program!"); // Print a message about the main method System.out.println("This program starts execution from the main method.");
🌐
Coderanch
coderanch.com › t › 779973 › java › Understanding-main-method-String-args
Understanding the main method and String[] args (Beginning Java forum at Coderanch)
February 27, 2024 - There is a convention in Java that the JVM would start a Java program by searching for and calling a method with the following signature: public static void main( String[] args ) The language designers could have called this anything they wanted but this is what they came up and we have to follow the convention.
🌐
Medium
medium.com › @sunkavallisowjanya › what-does-public-static-void-main-string-args-df5790cb037c
What does public static void main(String [] args){} mean?🤔 | by Sowjanya Sunkavalli | Medium
August 20, 2022 - So simply defining public is an access specifier, static is a Keyword, the void is the Return Type, main is the default method name and String args [] is the array of type string.