Java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Java

Why is the main method so important ?

14th Jun 2018, 5:55 PM
Kelvin A
Kelvin A - avatar
3 Answers
+ 3
Main Method is very important to start our code Execution , when we start writing code then we have write first line “public static void main (String Args[])” , This is only because java compiler don’t Understand any thing excluding Main method and as soon as compiler get this method they it start execution in normal flow , Directly say , if you will not write this main method in your code then your code will never be compiled and throws Exception Like , Error: Main method not found in class Simple, please define the main method as: public static void main(String[] args) So you have to define main method in your code . Its not Enough to write only main method in your code , you have to understand the syntax and terms which should be in your main method . “public static void main (String Args[])” public :- public is modifier which used here to provide accessibility to the method so that it can be access any where from you system . https://www.quora.com/Why-is-the-main-method-in-Java-important
14th Jun 2018, 5:58 PM
Alexander Sokolov
Alexander Sokolov - avatar
+ 15
lets keep it simple there must be a starting point from where the statements starts getting executed ... so we need something that can define the starting point ... and thats the role of main method
14th Jun 2018, 6:14 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
0
Alexander Sokolov Actually java compiler doesn't care about main method and it will compile your program even if it doesn't contain main method or wrong prototype of main. It's the JVM who executes the main method in the class which you load. So, back to original question why main method is important? One method had to be fixed from where execution of the program will start.
14th Jun 2018, 7:00 PM
Nikhil Sharma
Nikhil Sharma - avatar