Why we use static in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why we use static in java?

While writing any java program....why we write...public static void main(String args[]){}

29th Sep 2019, 6:23 PM
SUTRAYE LOKESH
SUTRAYE LOKESH - avatar
1 Answer
+ 4
Because we don't want to share main method with class objects. If main method was not declared as static it would be declared as non-static by default, non-static method are able to get called by class objects. We don't want the main method to get called by objects. Even though we didn't call main by objects, declaring the method as non-static would waste recourses for method to be prepared to get called by class objects, which won't be done.
29th Sep 2019, 6:49 PM
Seb TheS
Seb TheS - avatar