Understanding public static void main(String[] args) in Java
In Java programs, the point from where the program starts its execution or simply the entry point of Java programs is the main() method. Hence, it is one of the most important methods of Java and having proper understanding of it is very important.
Most common syntax of main() method:
class GeeksforGeeks { public static void main(String[] args) { System.out.println("I am a Geek"); } } |
chevron_right
filter_none
I am a Geek
Explanation:
Every word in the public static void main statement has got a meaning to the JVM.
- Public: It is an Access modifier, which specifies from where and who can access the method. Making the main() method public makes it globally available. It is made public so that JVM can invoke it from outside the class as it is not present in the current class.
classGeeksforGeeks {privatestaticvoidmain(String[] args){System.out.println("I am a Geek");}}chevron_rightfilter_noneError: Main method not found in class, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application
- Static: It is a keyword which is when associated with a method, makes it a class related method. The main() method is static so that JVM can invoke it without instantiating the class. This also saves the unnecessary wastage of memory which would have been used by the object declared only for calling the main() method by the JVM.
classGeeksforGeeks {publicvoidmain(String[] args){System.out.println("I am a Geek");}}chevron_rightfilter_noneError: Main method is not static in class test, please define the main method as: public static void main(String[] args)
- Void: It is a keyword and used to specify that a method doesn’t return anything. As main() method doesn’t return anything, its return type is void. As soon as the main() method terminates, the java program terminates too. Hence, it doesn’t make any sense to return from main() method as JVM can’t do anything with the return value of it.
classGeeksforGeeks {publicstaticintmain(String[] args){System.out.println("I am a Geek");return1;}}chevron_rightfilter_noneError: Main method not found in class, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application
- main: It is the name of Java main method. It is the identifier that the JVM looks for as the starting point of the java program. It’s not a keyword.
classGeeksforGeeks {publicstaticvoidmyMain(String[] args){System.out.println("I am a Geek");}}chevron_rightfilter_noneError: Main method not found in class, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application
- String[] args: 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 user can use any name in place of it.
classGeeksforGeeks {// javac GeeksforGeeks.java// java GeeksforGeeks 1 2 3publicstaticvoidmain(String[] args){for(String elem : args)System.out.println(elem);}}chevron_rightfilter_none1 2 3
- Understanding "static" in "public static void main" in Java
- Static and non static blank final variables in Java
- Difference between static and non-static variables in Java
- Difference between static and non-static method in Java
- Java.lang.Void Class in Java
- Replacing 'public' with 'private' in "main" in Java
- Understanding for loops in Java
- Understanding Classes and Objects in Java
- Understanding OutOfMemoryError Exception in Java
- Understanding Array IndexOutofbounds Exception in Java
- static keyword in java
- Static blocks in Java
- Static class in Java
- Static import in Java
- Shadowing of static functions in Java
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




