Monday, 28 September 2015

What is public static void main() in JAVA

public-static-void-main
Since we have already read about basic Java programs in previous posts and we have seen that a line public static void main(String args[]).
So what is the use of it ? and why we are using it? Lets see about it in detail.
This statement is an important statement that will be present in all JAVA programs.
Even if a program contains many classes then the class which contains this public static void main(String args[]) is considered as main class.
This is the most important compulsory statement in all JAVA programs. The program execution will start from this method only.
To know the meaning of this statement first we just want to know the meaning of each of the word in this line.
First “public”
Public is the visibility mode that was assigned to the method “main()”. Hence it is declared as public so anyone can be allowed to access this method.
main() method must be declared as public, because it must be called when program starts. Various types of visibility modes will discussed in the next tutorials.
“static”
Static is a keyword. This keyword allows the programmer to call the function by using the class name rather than using object name. In JAVA programming language the normal method for calling method and variables can be done using objects but this static keyword reduces the creation of objects for calling them, Because “main()” method is called by the operating system, in operating system there is no such environment for calling methods using object creation, so that JAVA provides effective way of calling methods by the rest of object creation.
In other words Static is a keyword which is used in front of a class or method so that the method or class cannot be changed. So that it is defined for some specified sort of work.

“void”
void is the return type for the method “main()”. Hence main method is declared as void, it does not return anything.
“main(String args[])”
“main()” is the name of the method, and String is the type of the parameter args. This args can be any other words such as arg . Because of this in JAVA programming language all types of inputs are taken as String format. So if we want to do some process on other data types such as integer , float we have to convert it into string type.
Thus the statement public static void main(String args[]) is very important.