Why do we put the word static with main method in a Java program?? Can anyone explain with an example please??
I'm not sure how to explain with an example since your question is the example lol :P but anyways, the reason why we put static within the main method in Java programs is because the word static just means that there will be only instance of that object in the class. To make it easier to understand, if you didn't have static declared, it would create a new copy of that object every time that class was initialized. This also benefits in time with the program since you can call them directly without having to initialize the class every time. This is why the main method is declared as static so that java doesn't have to initialize it to accomplish the main function. BTW, if you have learned methods, most methods start with static such as: public static int getNum (int n) { code }
Join our real-time social learning platform and learn together with your friends!