Why main method is defined public static | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why main method is defined public static

public static void main(String[] arcs) {}

18th Feb 2018, 11:58 AM
Ravi Kumar
Ravi Kumar - avatar
2 Answers
+ 8
[ public ] Because the program entry point needs to expose to the public so it can be invoked/called and run properly. [ static ] Because the program was invoked/called without its class instantiated during runtime. You may imagine the program was called like this MyClass.main(); instead of this MyClass obj = MyClass(); obj.main(); P/S: This question has been asked so many times so please use the search in Discussion next time.
18th Feb 2018, 12:51 PM
Zephyr Koo
Zephyr Koo - avatar
+ 4
Based on Zephyr Koo's answer, If main method is not declared static, you need to create object so that your problem can run successfully😂
18th Feb 2018, 12:54 PM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar