When the JVM calls the main() method, the Java program begins execution. Java applications start with this method. Without the main method, the Java file compiles successfully because at compile time, the compiler does not check the main method, but at runtime the JVM checks if the main() method is available. Therefore, we will get an exception at runtime.
In this article, we will learn why we should follow the convention "public static void main(String[] args)."
public class class_name { // This line must be written as it is public static void main(String[] args) { // code will be wriiten here } }The Chinese translation of
public class Tutorialspoint { public static void main(String []args) { System.out.println("Hello, you are on tutorials point"); } }
Hello, you are on tutorials point
In our java file, there should be at least one public class available. By convention, the main method must be defined in a class, because in Java everything is inside the class. This shows that java is an object-oriented language.
In the above example, the class 'Tutorialspoint' contains the main() method. Let’s discuss the various parts of the main() method −
public is an access modifier used to define the visibility and accessibility of variables and methods. Variables and methods defined using the public keyword can be accessed by any class or package. Previously we discussed that the JVM calls the main() method, which is not located in the current class. Therefore, main() method is declared as public so that we can access it in global scope or anywhere.
What happens if we don’t use the public keyword in the main() method?
public class Tutorialspoint { static void main(String []args){ System.out.println("Hello, you are on tutorials point"); } }
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 encountered this error because the JVM could not find the main() method.
Normally, we call methods by creating an object of a class, but static methods can be called without using an object. JVM calls the main() method before creating any object, that's why we need to declare it as static.
The Chinese translation ofMost members of the built-in class Math are static. We can use them directly without creating objects.
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) ); } }
Ceil value of x: 7.0 Floor value of y: 4.0
The above example demonstrates the use of the static methods ceil() and floor() of the Math class. We can see that they are used directly in our program without creating any objects of the Math class.
Let's see what happens if we don't declare the main() method as static.
The Chinese translation ofpublic class Tutorialspoint { public void main(String []args){ System.out.println("Hello, you are on tutorials point"); } }
Error: Main method is not static in class Tutorialspoint, please define the main method as: public static void main(String[] args)
JVM cannot call the above code.
The return type void indicates that the method does not return anything. In Java, the main() method is the entry and exit point of the program. When the main() method completes execution, the Java program also ends execution. If we provide any return type like int or double, it will throw an error at compile time.
Let us understand through an example -
public class Tutorialspoint { public int main(String []args){ System.out.println("Hello, you are on tutorials point"); } }
Tutorialspoint.java:4: error: missing return statement } ^ 1 error
This is a compile-time error. The compiler requires a return statement, but it is of no use to the JVM.
main is the name of the method, do not mistake it for the keyword. It is always written in the form "main".
The Chinese translation ofString[] args is a parameter that accepts String type parameters. It allows us to pass parameters through the terminal and store these parameters in a string array. We can say that String[] args is a command line argument.
This example will illustrate how to pass parameters 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 following command: javac Arg.java
To run the code from the terminal, enter the following command: java Arg "Your String"
PS D:\Java Programs> java Arg "Hello, You are on Tutorials Point" Hello, You are on Tutorials Point
In this article, we discussed the Java program for understanding "public static void main(String[] args)". We also discussed how to receive input from the terminal using the parameters ‘String[] args’.
The above is the detailed content of Java public static void main(String args) Java public static void main function (String parameter). For more information, please follow other related articles on the PHP Chinese website!