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?
The
main(String[])
method has a specific prototype that is dictated by how the Java runtime environment works. When you invokejava MyApplication
from the command line, the Java VM will look for a staticmain(String[])
method contained in that class in order to execute the application. If that method is not found, then the Java VM can't run the class as an application. That's just how the language is defined. It also means that the Java VM doesn't create an instance of your application class in order to run it.Now, if you want your class to be usable either as a standalone application or as an instance that's created by something else, then you can have your class implement the
Runnable
interface, and also provide amain
method that executes therun
method on a new instance.Now any class could potentially create an instance of
MyRunnableThing
and use itsrun()
method to produce the same behavior that would have been seen by executingjava MyRunnablething
.See also: Working with Static Constructor in Java. Some highlights from that Q&A:
Java does not permit to declare a constructor as static. Following are the reasons.
Static means for the same class. i.e, static methods cannot be inherited.
With static, "this" reference (keyword) cannot be used. "this" is always linked to an object. A constructor always belongs to some object.
If a constructor is static, an object of subclass cannot access. If static is allowed with constructor, it is accessible within the class but not by subclass.
The purpose of Constructor is to Construct an Object i.e. to initialize class's instance variables either their default values or by their initialized values. non-static Instance variables can't be accessed by static methods . So constructor is not static.
A constructor cannot be static, because in an OO language, the process for creating an object is as follows:
Constructors are not used anywhere else (and a type-safe language should enforce this), so it follows that a constructor will always be called in a non-static context.
If a constructor were static, it would not receive a reference to the newly-allocated object, and thus would not be able to initialise it.
Thus, a constructor can always be non-static (as it is always called from a non-static context) and must always be non-static (otherwise it would be unable to perform its task).
Constructor is used to create Objects.
Static is generally which is same for all objects.
So, if we have had static constructors creation of one object would affect all the other existing objects.
Static methods only reference to static variables. Therefore all the initial parameters which you are giving to create an object would change for all objects. It is no point creating similar objects for no use.
Hope this helps.... :)
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
main
method. 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.