In Java program we always use public static void main() method. To understand the use of this function we have to consider on some topic, JVM, public and static keyword.
JVM is a Java Virtual Machine which loads the byte-code (compiled form of our java program which is used by JVM in different platform to run the java program) and other related libraries from JRE (Java Run-time Environment).
Public and static are two keywords available in Java. Public is an access specifier keyword which allows to access particular instance variable and method from outside. Static is a keyword which makes the instance variable static i.e: static instance variable and methods are independent from objects and can be accessed without creating objects or through class name.
In java everything is Object oriented therefore we need to create an object of everything to fulfill our requirements. Creation of objects and execution of methods are redirected through main() method.
Therefore without a main() method we can do nothing.
When we try to execute our java program, JVM loads our class file of our program and try to access and execute main() method. If the main method is not declared as public static then JVM cannot execute our program. To enable the JVM to access main() method we have to declare the main function as public static which enables the JVM to execute the main() method before creating objects of any classes in our program. Public keyword makes the method available for everyone and static keyword makes it possible to access and execute the method through the class name. JVM access the main() method through class name and execute all the statements inside main() method.
No comments:
Post a Comment