Home > DeveloperSection > Beginner > What is “public static void main(String args[])”

What is “public static void main(String args[])”


Java Java 
Ratings:
0 Comment(s)
 814  View(s)
Rate this:

What is “public static void main(String args[])”

 

Public

 

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.

 

Static

 

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.

The keyword 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.

 

Void

 

Since Java is platform independent language and if it will return some value then the value may mean different things to different platforms.

The keyword 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()

 

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.

Every Java application must contain a main method whose signature looks like this:

 public static void main(String[] args)

 

How the main Method Gets Called

The main 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 application.

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 not defined

 

String args[]

 

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. 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 Method

As you can see from the following code snippet, the main method accepts a single argument: an array of elements of type String.

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.


Don't want to miss updates? Please click the below button!

Follow MindStick