Please Java developers should help me with this. What is the necessity of static being in the main function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Please Java developers should help me with this. What is the necessity of static being in the main function?

As said by sololearn and some other Tutorial sites, Public methods can only be accessible buy an object, while static methods can be called without creating an object. In java static and public must always be in the main method. The way the method is called by the program will determine which of the keywords is used. So why must the two be important when only one is used or is it the two that is used? If so why is it? Thanks for your answers.

25th Sep 2020, 7:17 PM
Joseph Onipede
Joseph Onipede - avatar
2 Answers
+ 5
In order to make it easier for the JVM to locate and invoke the main method, it is made public and static. Because public means global access and static means that you need not create an instance of the class to call the main method. You *must* use both the keywords because the main method is an entry point of the program and it must match the right method signature.
25th Sep 2020, 7:27 PM
Avinesh
Avinesh - avatar
0
I'm not sure if I understand your question. If you're asking why main needs to be static, it's because otherwise main would have to instanced as an object to start executing code. How do we instance an object? Through the execution. Main is the only method which tells the compiler where to start execution. Your code will not break if you make main not static, but nothing will execute.
25th Sep 2020, 7:33 PM
Odyel
Odyel - avatar