main( ) method is important in java as every program has to begin with it. Let us see in detail about the main( ) method in java.
main( ) method:
- The access specifier 'public' denotes that the main() method is accessible to all other classes.
Why should the main( ) method be public?
For the reason that it must be called by the code outside of its class 'Sample' when the program is started. If it is private, or default, it can't be accessed across the package and hence the java interpreter can't invoke it.
- Why 'static'?
main( ) method is inside the class sample. Had it been defined as a non-static member of the class, an object of class 'Sample' will be required to invoke it.But, as the interpreter needs to invoke this method even before any objects are created, the only other way to invoke the main( ) method would be to use the class name. Using the class name is possible only if main( ) method is defined as the static member of class 'Sample'.
- Void( )
To denote that main( ) method does not return any value.
- String args[ ]
An array of objects of the class type String. If any command line arguments are specified after the class name when invoking the Java application, the runtime system passes them to the application's main method via this array of Strings.