+ 3

Why the main method in java is public and static?

20th Jul 2017, 1:26 AM
Sri Datta
Sri Datta - avatar
2 Réponses
+ 1
It's public so that it may be called from outside the class. It's static because otherwise you'd need to create an instance of the class first. There are other reasons, but once you've learned about access modifiers and static, this should make perfect sense to you. https://stackoverflow.com/questions/22328302/why-main-is-declared-public-and-static-in-java
20th Jul 2017, 2:06 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
It is public beacuse at execution compiler calls it from outside of the class and it is static beacause compiler calls it without making the instance of class
20th Jul 2017, 4:00 AM
Jonty Bamrah