The Java main
method is usually the first method you learn about when you start programming in Java because its the entry point for executing a Java program. The main
method can contain code to execute or call other methods, and it can be placed in any class that’s part of a program. More complex programs usually have a class that contains only the main
method. The class that contains the main
method can have any name, although typically you can just call the class Main
.
In the examples that follow, the class that contains the main
method is called Test
:
In this article you’ll learn what each component of the main
method means.
The syntax of the main
method is always:
You can change only the name of the String
array argument. For example, you can change args
to myStringArgs
. The String
array argument can be written as String... args
or String args[]
.
public
The access modifier of the main
method needs to be public
so that the JRE can access and execute this method. If a method isn’t public, then access is restricted. In the following example code, the main
method is missing the public
access modifier:
When you compile and run the program, the following error occurs because the main
method isn’t public and the JRE can’t find it:
OutputError: Main method not found in class Test, please define the `main` method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
static
When the Java program starts, there is no object of the class present. The main
method has to be static
so that the JVM can load the class into memory and call the main
method without creating an instance of the class first. In the following example code, the main
method is missing the static
modifier:
When you compile and run the program, the following error occurs because the main
method isn’t static
:
OutputError: Main method is not static in class Test, please define the `main` method as:
public static void main(String[] args)
void
Every Java method must provide the return type. The Java main
method return type is void
because it doesn’t return anything. When the main
method is finished executing, the Java program terminates, so there is no need for a returned object. In the following example code, the main
method attempts to return something when the return type is void
:
When you compile the program, the following error occurs because Java doesn’t expect a return value when the return type is void
:
OutputTest.java:5: error: incompatible types: unexpected return value
return 0;
^
1 error
main
The Java main
method is always named main
. When a Java program starts, it always looks for the main
method. The following example code shows a main
method renamed to myMain
:
When you compile and run the program, the following error occurs because the JRE can’t find the main
method in the class:
OutputError: Main method not found in class Test, please define the `main` method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
String[] args
Java main
method accepts a single argument of type String
array. Each string in the array is a command line argument. You can use command line arguments to affect the operation of the program, or to pass information to the program, at runtime. The following example code shows how to print the command line arguments that you enter when you run the program:
When you compile the program and then run it with a few command line arguments separated by spaces, the arguments get printed in the terminal:
Output1
2
3
Testing the main method
In this article you learned about each component of the Java main
method. Continue your learning with more Java tutorials.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Nice explanation on core main method.
- Sambasiva
Well said Sambasiva !!!
- Rahul
Excellent! Given more clarity on main method
- Vinodkumar
Sir I want to implement push notifications like your site. My website is built on spring and jsp. Please help me out of this.
- Abhishek Tripathi
I am using OneSignal push notification and they have a WordPress plugin, they also have Java API. Please look into their documentation for implementing it. You can also look at some other services for push notification.
- Pankaj
not working
- Priyanka
what do you mean?
- Pankaj
//run this code and see the same error at run time. your idea fails public class aggregation { String name; int roll; String bg; int marks; aggregation(String name, int roll, String bg, int marks) { this.name=name; this.roll=roll; this.bg=bg; this.marks=marks; }} class students { int salary; String gender; aggregation info; students(int salary, String gender, aggregation info) { this.salary=salary; this.gender=gender; this.info=info; } public static void main(String[] args) { aggregation a1=new aggregation(“Upendra Dhamala”, 48, “B+”,98); students a2=new students(90000, “M”, a1); System.out.println(a2.salary); System.out.println(a2.gender); System.out.println(a2.info.name); System.out.println(a2.info.roll); System.out.println(a2.info.bg); System.out.println(a2.info.marks); } }
- upendra
Nice explanation on core main method.
- Naveen singh
Can you explain as to why do we need to have a String args[] array passed to main? Why does it not compile if we do not pass a String args[] array? Why does Java need a String args[] array?
- Duke
It’s the syntax and to pass command line arguments to the main method.
- Pankaj
Sir I still haven’t understood the significance of String [] args. Please explain.
- Kanchan Gautam
I think there is a small mistake in saying "Java programming mandates that every method signature provide the return type. ". Return type is not part of the method signature. Nice explanation though!
- Cacarian
Thanks for noticing the typo. Java programming mandates to have a return type, but yes it’s not part of the method signature.
- Pankaj
Very Nice explanation about java main method…each part…Great
- Vijay
awsome explanation sir,keep going ,
- p.naveen
Sir i am new to java . I have compiled and run about 30 java programe but now in a specific folder java programe comiled but wont run showing error main method not found even i have main method psvm(S[] args).but when i run same programe in other folder it runs .why that only folder its not running .pls help me
- Ranjan
can any one suggest me how to study or how to understand threads in java, because i studied so many times , but i’m unable to get the clarity about the concept and coding.
- Nanda
check your package
- Jilson
What about adding throws Exception() in the definition, doesn’t it change the signature of the function?
- Samridhi Jain
No, throwing exceptions is not part of the signature.
- Pankaj
Hi, Great article, I will share my thoughts. As per JLS (Java Language Specification), “A Java virtual machine starts execution by invoking the main() method of some specified class, passing it a single argument, which is an array of strings”. Definition of your main method should be as below public static void main(String[] args) public - your method should be accessible from anywhere static - Your method is static, you can start it without creating an instance. When JVM starts, it does not have any instance of the class having main method. So static. void - It does not return anything. Henceforth, the main() method is predefined in JVM to indicate as a starting point. Hope that helps!
- Ellen Dares
Awesome explanation
- Thomas L Dean
{ public static void main(int[] args) { System.out.println(“this is the overload of the method”); } public static void main(String[] args) { System.out.println(“this isn working anymore”); } output-this isn working anymore can you explain me why this output will be show…
- raghavendra jha
Refer https://stackoverflow.com/questions/3759315/can-we-overload-the-main-method-in-java
- Jagadeesh Keerthi
“this isn working anymore”- this output came bacause JVM never accepts the int[] args in main method. When you run a java program then JVM will search for main method in the class with String []args if it not present then it will not print anything. it will give an error like " Main method not found".
- Sawan sharma
two main functions present when there should be only one
- kamal
That’s amazing, thank you
- ofek Shaltiel
nice explanation about each part thank you!..
- DileeP Achar
public is access specifier or access modifier . i think it is access specifier but in the explanation you given it as access modifier.
- satyanarayana
Both access modifier and specifier have same meaning.
- kamal
Why do you sometime type: public static void mymain(String args[ ]) and other times: public static void mymain(String [ ] args) ? What is the correct sintax? Thanks, Miguel Ferreira
- Miguel Ferreira
Hello Sir, I have a query about 2 main method in java? I was written 2 main method in single main class,But i am not getting 2 output like(Show screen,sucess shows). you can see the program below? --------------------------------------------------------------------------------- class Module{ public void Dress() { System.out.println(“Shows Screen”); } } public class JavaObject3 { public static void main(String[] args1) { Module m=new Module(); m.Dress(); } public static void main1(int args) { JavaObject3 obj1=new JavaObject3(); System.out.println(“sucess shows”); } }
- arshad khan
help a lot…thank you so much.
- SITI NUR ADILAH SULAIMAN