What is “public static void
It is the
access modifier that means main() method can be accessed from everywhere. The
public keyword, allows the programmer to control the visibility of class
members. When a class member is preceded by public, then that member can 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 code outside of its
class when the program is started.
It means that you can call this
method from outside of the class you are currently in. This is necessary
because this method is being called by the Java runtime system which is not
located in your current class.
It is the access
modifier which means we can call a method directly using class name without
creating an object of it.
Static is a special keyword that indicates that this method can be called
without creating an instance of this class. Without it, you have to instantiate
this class and call this method from the resulting object.
static allows main( ) to be called without having to instantiate a particular
instance of the class. Before the main method is called, no objects are
instantiated. This is necessary since main( ) is called by the Java
interpreter before any objects are made.
is platform independent language and if it will return some value then the
value may mean different things to different platforms.
void simply tells the compiler that main( ) does not return a value. As you
will see, methods may also return values of different datatypes.
main( ) is
the method called when a Java application begins. main() method is the default
entry point for a program. Keep in mind that 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 the 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. However, the Java interpreter would
report an error because it would be unable to find the main( ) method.
application must contain a main method whose signature looks like this:
public static void main(String args)
How the main Method Gets Called
method in the Java language is similar to the main function in C and C++. When
the Java interpreter executes an application (by being invoked upon the
application's controlling class), it starts by calling the class's main method.
The main method then calls all the other methods required to run your
If you try
to invoke the Java interpreter on a class that does not have a main method, the
interpreter refuses to compile your program and displays an error message
similar to this:
In class NoMain: void main(String args) is
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. If there are no parameters required for
a given method, you still need to include the empty parentheses. In main( ),
there is only one parameter. String args[ ]
declares a parameter named args, which is an array of instances of the class
String. Objects of type String store character strings. In this case, args
receives any command-line arguments present when the program is executed.
Arguments to the main
As you can see from the following code
snippet, the main method accepts a single argument: an array of elements of
public static void main(String args)
This array is the mechanism through
which the runtime system passes information to your application. Each String in
the array is called a command-line argument. Command-line arguments let users
affect the operation of the application without recompiling it. For example, a
sorting program might allow the user to specify that the data be sorted in
descending order with this command-line argument.