Java | Understanding signature of main() method

| By Webner

The syntax of main method in java is:

public static void main(String[] args) {

There are three keywords used in main: public, static, void

public: public keyword is the access modifier in Java which gives its access to the outside world. As class execution begins from main method therefore in order to access the class it is necessary to make main method as public so that the outside world could use it. If we use ‘private’ access modifier along with main that means we are restricting its accessibility to ourselves and it will not be available to the outside world therefore there is no significance of creating this type of class which cannot serve outside.

static: static keyword is used for the class members. Class members means which belong to the class not its objects. main method is the method that belongs to class i.e we need not create the object of that class to execute it.

As everything in Java is in classes so if we do not declare main method as static then it could be invoked only by object of its class. If the object of that class is not created then the class will not even execute.

For Example: On typing, java ExampleClass at the console (ExampleClass is the name of the class which we are executing ) JVM loads ExampleClass class and searches for ExampleClass.main() method without creating any object of it.

Another case is that if we have declared all the Constructors as private then on declaring main method as non static it will be impossible to execute that class as its object cannot be created:

class ExampleClass{
private ExampleClass(){}

Now as this class has private constructor, therefore, we cannot create the object of this class. In this case also, we can execute this class only if its main method will be static.

void: void is the keyword that is used with methods when they return nothing. As in Java, return type is must to write along with each method therefore ‘void’ is written with main method because main method does not return anything and it is used mainly to begin execution of the class.

String[] args: the main method also takes parameters as String[] args where args is just a name of the parameter, we can use any name instead of that like:

public static void main(String[] string_array_name){

Here String[] is the string array that takes the input from the user through command prompt.

For Example: if we type java ExampleClass 10 20 at the command prompt then it will take 10 and 20 as the values for this String array which we can use in our class by first parsing them to appropriate type like:

public class ExampleClass {
public static void main(String args[]){
int i=Integer.parseInt(args[0]); //parsing string to int
System.out.println(“value of parameter i is ”+i);

Execute: java Main 10

Output: value of parameter i is 10

We can also write static public void main(String[] args) instead of public static void main(String[] args). It does not generate any error but it is a convention to write access modifier first.

Leave a Reply

Your email address will not be published. Required fields are marked *