define public static void main(String[] args)?

The entry point for the execution in all programming language is the main().
so the question is that why we prefix out "main()" by "public" and "static"?
                                -Answer is that firstly it is the prototype and jvm only search for the main() which is public and static and have the String[] argument.
                                 -Another reason of it is that when we create our programs in any package (if we don't declare any package in our program then it should be consider as in the default package by jvm)
then the only public methods can only be accessed by the caller (in the case of main() caller is jvm)
if the caller is at the another package then the program (program and caller are in the different locations), jvm is not a single unit it comprises of many classes of jdk so its location is not in a package so that we have to set our main() as public so that jvm can access it, if our program in any package in our system.
                               -static methods are the methods which are accessible by the caller by class name, their is no need to create object and use object to call the static method. 
                               -jvm cann't create the object of any class implicitly only the programmer can explicitly creates the object in the programs. that's why we create main() as static so that it can be accessed by the jvm without object.

Comments

Popular posts from this blog

Inter-Thread communication

define System.out.println()?

Reflection in java