I do not understand why the main method has to be static. I understand static variables but static methods are difficult for me to grasp. Do static method exists so that one can create two methods with the same name in two different classes that won’t clash with each other?
Also, I don’t understand why I can’t create a static constructor.
Could anyone help explain this concept?
Java has
[static constructors]static initialization blocks which can be viewed as a “static constructor”:In any case, the only method in the main class which must be static is the
mainmethod. This is because it is invoked without first creating an instance of the “main class”. A common technique, and the one I prefer, is to quickly get out of static context:Also, static has nothing to do with “name clashes”. A static method (or variable) is simply a method (or variable) that is not associated with a specific instance of a type. I would recommend reading through the Classes and Objects Java Tutorial and the section Understanding Instance and Class Variables.
Happy coding.