Here is a convincing post why main() is public static void. First of all it is important to know who executes the main().
The main() method is executed by the Java Virtual Machine in the system. Almost every system do have it.
The main() is made public because it is made to be accessed by the JVM. If it private it is restricted to the class, if protected circumscribed to it's sub classes also.
The main method is static because it needs to be accessed without the need of creating an object. As JVM itself cannot create any objects. One of the reasons for this is that JVM doesn't know what parameters to send if the constructor has parameters. And one more thing is that main() is the first to execute and it has to be executed before any statement executes. Therefore, the statement of creation of object of that class cannot come first. And even the JVM doesn't know if the programmer wants to create an object for that class!, what if it is an abstract class otherwise? There are a lot of reasons, why main is static..
The main is void because, there is no need to tell to OS that the job is over. The Java's strong memory management does it all. Whenever, the cursor reached the end of the main, the job is over and all the allocated resources are be de-allocated.
The arguments are used to pass any arguments to the program. For instance, when you are working with Open with.. in right click context menu of the explorer, you choose a program for it, then the path of the file on which you right click will be transferred to the respective program's main method as an argument. The same happens for Java too
The main() method is executed by the Java Virtual Machine in the system. Almost every system do have it.
The main() is made public because it is made to be accessed by the JVM. If it private it is restricted to the class, if protected circumscribed to it's sub classes also.
The main method is static because it needs to be accessed without the need of creating an object. As JVM itself cannot create any objects. One of the reasons for this is that JVM doesn't know what parameters to send if the constructor has parameters. And one more thing is that main() is the first to execute and it has to be executed before any statement executes. Therefore, the statement of creation of object of that class cannot come first. And even the JVM doesn't know if the programmer wants to create an object for that class!, what if it is an abstract class otherwise? There are a lot of reasons, why main is static..
The main is void because, there is no need to tell to OS that the job is over. The Java's strong memory management does it all. Whenever, the cursor reached the end of the main, the job is over and all the allocated resources are be de-allocated.
The arguments are used to pass any arguments to the program. For instance, when you are working with Open with.. in right click context menu of the explorer, you choose a program for it, then the path of the file on which you right click will be transferred to the respective program's main method as an argument. The same happens for Java too
No comments:
Post a Comment