Java public static void main(String[] args)


The java programs start execution when JVM calls the main() method. Java application begin from this method. Without main method, a java file will compile successfully because at compile time, compiler doesn’t check for main method but at run time JVM checks whether the main() method is available or not. Therefore, we will get an exception at run time.

In this article, we will understand why we follow the convention “public static void main(String[] args).”

Syntax

public class class_name {
   // This line must be written as it is
   public static void main(String[] args) {  
      // code will be wriiten here
   }
}

Example 1

public class Tutorialspoint {
   public static void main(String []args) {
      System.out.println("Hello, you are on tutorials point");
   }
}

Output

Hello, you are on tutorials point

In our java file, at least one public class should be available. By convention, the main method must be defined within a class because everything comes inside a class in java. It shows that java is an object-oriented language.

In the above example, The class ‘Tutorialspoint’ contains main() method. Let’s discuss the parts of main() method −

public

Public is an access specifier that defines visibility or accessibility of a variable and method. The variables and methods defined using public keyword are accessible to any class and package. Earlier, we have discussed that JVM calls the main() method and it is not located in the current class. Therefore, main() method is declared as public so that we can access it globally or from anywhere.

What if we don’t use the public keyword along with main() method?

Example 2

public class Tutorialspoint {
   static void main(String []args){
      System.out.println("Hello, you are on tutorials point");
   }
}

Output

Error: Main method not found in class Tutorialspoint, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

We are getting the error because JVM is unable to find main() method.

static

Generally, we call a method by creating an object of its class but static methods can be called without using objects. The JVM calls the main() method prior to the creation of any object that’s why we need to declare it as static.

Example 3

Most of the members of inbuilt class Math is static. We can use them directly without creating its object.

public class Main {
   public static void main( String[] args ) {
      double x = 6.55;
      double y = 4.32;
      System.out.println(" Ceil value of x: " + Math.ceil(x) );
      System.out.println(" Floor value of y: " + Math.floor(y) );
   }
}

Output

 Ceil value of x: 7.0
 Floor value of y: 4.0

The above example illustrates the use of static methods ceil() and floor() of Math class. We can see that we have used them directly in our program without creating any object of Math class.

Let’s see what will happen if we don’t declare main() method as static.

Example 4

public class Tutorialspoint {
   public void main(String []args){
      System.out.println("Hello, you are on tutorials point");
   }
}

Output

Error: Main method is not static in class Tutorialspoint, please define the main method as:
   public static void main(String[] args)

The JVM is not able to call the above code.

void

The return type void signifies that the method doesn’t return anything. In java, the main() method is the entry as well as exit point of program. When main() method finished its execution the java program also ends execution. If we provide any return type like int or double then it will throw an error at compile time.

Let’s understand with an example −

Example 5

public class Tutorialspoint {
   public int main(String []args){
      System.out.println("Hello, you are on tutorials point");
   }
}

Output

Tutorialspoint.java:4: error: missing return statement
    }
    ^
1 error

It is a compile-time error. The compiler is asking for a return statement but it is of no use for JVM.

main()

main is the name of method don’t mistake it for a keyword. It is always written like ‘main’.

String[] args

String[] args is a parameter that accepts String type arguments. It allows us to pass arguments through terminal and it stores these arguments in an array of strings. We can say that String[] args is a command line argument.

Example 6

This example will illustrate how we can pass argument from terminal to a java file.

public class Arg {
   public static void main(String []args){
      // for each loop to print argument taken from terminal
      for(String arg : args) {  
         System.out.println(arg);
      }
   }
}

Output

To compile the code from terminal type command: javac Arg.java

To run the code from terminal type command: java Arg “Your String”

PS D:\Java Programs> java Arg "Hello, You are on Tutorials Point"
Hello, You are on Tutorials Point

Conclusion

In this article, we have discussed the java programs to understand the use of “public static void main(String[] args).” We also discussed how we can take input from terminal using the parameter ‘String[] args.’

Updated on: 02-May-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements