Translate

Tuesday 12 June 2012

Java's main() method

public static void main(String args[])
This line begins the main() method. All java applications begins execution by calling main(). The public keyword is an access specifier, which allows the programmer to control the visibility of class members. When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared. In this case, main() must be declared as public, since it must be called by the code outside of its class when the program is started. The keyword static allows main() to be called without having to instantiate a particular instance of the class. This is necessary since main() is called by Java interpreter before any objects are made. The keyword void simply tells the compiler that main() does not return a value. As stated, main() is the method called when a Java application begins. Java is case-sensitive. Thus, Main is different from main. It is important to understand that the Java compiler will compile classes that do not contain a main() method. But Java interpreter has no way to run these classes. So, if you had typed Main instead of main, the compiler would still compile your program. But the Java interpreter would report an error because it would be unable to find the main() method.
Any information that you need to pass to a method is received by variables specified within the set of parentheses that follow the name of the method. These variables are called parameters. In main(), there is only one parameter. String args[] declares a parameter named args, which is an array of instances of the class String. In this case, args receives any command-line arguments present when the program is executed.    

No comments:

Post a Comment