Java Public Static Void Main String Args



The Java program starts execution when the JVM calls the main() method. A Java application begins with this method.

Without a main() method, a Java file will compile successfully because at compile time, the compiler doesn't check for a 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)."

The Syntax of a basic Java program looks like:

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

In our Java file, at least one class should be declared public. By convention, the main method must be defined within a class because every attribute and property is placed inside a class in Java. It shows that Java is an object-oriented language.

Example: Basic Java Program

Now, we will see how to write a basic Java program that prints a "Hello" message on the console.

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

Following is the output of the above program -

Hello, you are on tutorials point

In the above example, the class Tutorialspoint contains main() method. Let's discuss the parts of main() method.

The public Keyword

Public is an access specifier that defines the visibility or accessibility of a variable and method. The variables and methods defined using the public keyword are accessible to any class and package.

Earlier, we 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.

Example

What if we don't use the public keyword along with the main() method:

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

We will get an error because the JVM is unable to find main() method.

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

The static Keyword

Generally, we call a method in Java 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: Use of a static Method

The methods of the built-in class Math are static. We can use them directly without creating their objects, as you can see in the example below:

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) );
   }
}

Following is the output of the above program -

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

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

Example: non-static main() Method

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

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

On running the above code, an error will occur as the JVM is not able to call the main() method.

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

The void Return Type

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 the program.

When the main() method finishes 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.

Example

Let's understand with an example:

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

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

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

The main() Method

The main is the name of the method, don't mistake it for a keyword. It is always written like 'main'.

The String[] args Parameter

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

Example

This example will illustrate how we can pass an argument from the 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);
      }
   }
}

To compile the code from the terminal, type the command: javac Arg.java. To run the code from the terminal, type the command: java Arg "Your String"

PS D:\Java Programs> java Arg "Hello, You are on Tutorials Point"
Hello, You are on Tutorials Point
Updated on: 2025-06-19T11:48:14+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started