The public static void main(String[] args) method in Java is the entry point for any Java program. The keyword public makes this method accessible from anywhere, ensuring the program can start executing from this point. static allows the method to be called without creating an instance of the class, making it easier to run the program directly. void indicates that the method doesn't return any value, and String[] args is an array that stores command-line arguments, allowing users to input data when they run the program. This setup is essential for Java programs to function correctly.
Syntax of Java Main() Method
The syntax for writing the method signature for the main class in java is as follows:
public static void main(String[] args)
Example of String[] args
Java
Java
class DemoApp{ public static void main(String args[]){ System.out.println("Hello World"); } }
You can also try this code with Online Java Compiler
In the above sample program, we have created a class named “DemoApp”. In this class, we are simply printing “Hello World”, but we are also writing a line “public static void main string args” in this code. So let us discuss the reasons for writing this line.
1. Public
The first keyword “public” is also called an access modifier in Java. It essentially tells the compiler about the scope of the function. The scope of the function means who can call this function. We want to make this function, that is, the “main” function, public so that when the Java Virtual Machine(JVM) compiles and calls the main(), by default, it can find the main() in our sample program.
Here is a program where “public” is removed:
Java
Java
class DemoApp{ static void main(String args[]){ System.out.println("Hello World"); } }
You can also try this code with Online Java Compiler
Error: Main method not found in class DemoApp, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx. application.Application
We get the following output if we omit the “public” keyword.
2. Static
The “static” keyword makes the main() a class function. By making it a class function, there is no need to create an object of that class to call the function. It is now possible for the JVM to call the main() without creating an object of the “DemoApp” class.
For example, when we remove the “static” keyword:
Java
Java
class DemoApp{ public void main(String args[]){ System.out.println("Hello World"); } }
You can also try this code with Online Java Compiler
Error: Main method is not static in class DemoApp, please define the main method as:
public static void main(String[] args)
3. Void
The “void” keyword mentions the return type of the function as nothing. The main() is the entry point of the program so we do not want to return anything, hence we must set the return type of the main() as void.
For example, when we remove the “void” keyword:
Java
Java
class DemoApp{ public static main(String args[]){ System.out.println("Hello World"); } }
You can also try this code with Online Java Compiler
DemoApp.java:2: error: invalid method declaration; return type required
public static main(String args[]){
^
1 error
4. Main()
The final keyword is “main”. In Java, we have a function named “main” as it will serve as the entry point for the program. The JVM, by default, will call main() when we run the compiled program. Thus, we keep the main() here as we are running this program through the JVM.
For example, when we remove the “main” keyword:
Java
Java
class DemoApp{ public static void(String args[]){ System.out.println("Hello World"); } }
You can also try this code with Online Java Compiler
We are also passing a parameter to the main(), that is “String args[]”. When we want to pass an argument to the main(), we can pass it through this parameter. The parameter is essentially an array of type “String”. We can pass arguments while running the program through the command line interface. The command line arguments separated by a space are taken in the array of Strings named “args”. We can access the arguments by simply indexing the “args” array as we do in Java. Arguments like file names or options for performing different operations can be passed as arguments in this manner.
For example, we pass in this program arguments “copy”, “filename1.txt” and “filename2.txt”. In the code below, we are traversing through all the strings passed as a command line argument in the for loop:
Java
Java
class DemoApp{ public static void main(String args[]){ for( String arg: args){ System.out.println(arg); } } }
You can also try this code with Online Java Compiler
Even when we have nothing to pass, it is important to write “String args[]” as a parameter as it is a part of the syntax in Java, and the program won’t run without it.
For example, when we remove the “String args[]” parameter:
Java
Java
class DemoApp{ public static void main(){ System.out.println("Hello World"); } }
You can also try this code with Online Java Compiler
Error: Main method not found in class DemoApp, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Overloading of main() method
Overloading of the main() method is allowed in Java, but it won't be the entry point of the program. The JVM always starts with the standard public static void main(String[] args) method. Other overloaded main() methods can be called regular methods. Let us look at the example of it:
Java
Java
public class MainMethodOverloading { public static void main(String[] args) { System.out.println("This is the standard main method."); // Calling overloaded main method with an String argument. NinjasClass.main("Rituraj Seal"); } }
class NinjasClass { public static void main(String name) { System.out.println("Overloaded main method with name argument: " + name); } }
You can also try this code with Online Java Compiler
In this example, we have two main() methods: the standard one and an overloaded one that takes a String argument. When we run the program, it starts with the standard main() method, but we also call the overloaded main() method from within the standard main() method.
What happens if the main() method is written without String args[]?
When we write the main() method without string args[], the code will compile but not run because the JVM fails to recognise the main() method. When we want to pass an argument to the main(), we can pass it through String args[]. JVM always searches for the main() function that accepts an array of strings as an argument. When you execute your programme, you can provide it a simple array of string arguments. String[] args must be provided or else a run-time error, or missing main method, will be generated.
Frequently Asked Questions
Why String args is used in Java?
String args in Java is used to pass command-line arguments to a Java program. It allows external data to be passed into the program when it starts, making it more versatile and adaptable to various inputs.
Why main () method is public static and void in Java?
main() is public static void for accessibility and to serve as the program's entry point without returning a value. public ensures that the method is accessible from outside the class. static method belongs to the class, not an instance of the class. void indicates that the main() method doesn't return any value.
What is the difference between public void and public static void in Java?
public void means the method is an instance method, requiring an object to be called. public static void means the method can be called without creating an instance, typically used for entry points like main().
Can we execute a Java program without the main method?
No, a Java program cannot be executed without the main() method unless it is a Java applet or uses another entry point defined by a framework.
Conclusion
In this blog, we discussed Java Main() Method - Public Static Void Main (String[] args). The main() method in Java, defined as public static void main(String[] args), is the cornerstone of any standalone Java application. It serves as the entry point for the program's execution, allowing the Java runtime to invoke it without requiring object instantiation. Understanding the components—public, static, void, and String[] args—is crucial for every Java developer, as this method facilitates seamless execution of code.
For further reading about Java, you can visit the following links -