Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Syntax of Java Main() Method
3.
Example of String[] args
3.1.
Java
3.2.
Public
3.3.
Java
3.4.
Output
3.5.
Static
3.6.
Java
3.7.
Output 
3.8.
Void
3.9.
Java
3.10.
Output 
3.11.
Main()
3.12.
Java
3.13.
Output 
3.14.
String Args[]
3.15.
Java
3.16.
Output 
3.17.
Java
3.18.
Output
4.
What happens if the main() method is written without String args[]?
5.
Overloading of main() method
5.1.
Java
6.
Frequently Asked Questions
6.1.
What is the public static void main String args?
6.2.
What is args in Java?
6.3.
Why String args is used in Java?
6.4.
Why main () method is public static and void in Java?
6.5.
What is the use of String args within main ()?
7.
Conclusion
Last Updated: May 22, 2024
Easy

Java Main() Method - Public Static Void Main (String[] args)

Author Ravi Khorwal
0 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

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.

public static void main string args

Syntax of Java Main() Method

The syntax for writing the method signature for the main class in java is as follows:

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Example of String[] args

  • Java

Java

class DemoApp{
public static void main(String args[]){
System.out.println("Hello World");
}
}

Output

Hello World


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.

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

Output

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.

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

Output 

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

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

Output 

DemoApp.java:2: error: invalid method declaration; return type required
        public static main(String args[]){
                      ^
1 error

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

Output 

DemoApp.java:2: error: <identifier> expected
       public static void(String args[]){
                         ^
1 error

String Args[]

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

Output 

PS C:\Users\USER\OneDrive\Desktop\Codeforces> javac DemoApp.java
PS C:\Users\USER\OneDrive\Desktop\Codeforces> java DemoApp copy filename1.txt filename2.txt
copy
filename1.txt
filename2.txt


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

Output

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 

 

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.

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

Output:

output

Explanation

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.

Frequently Asked Questions

What is the public static void main String args?

You can add a main function to a Java application by using the keyword public static void main. It accepts parameters for command-line processing but cannot return values. This is the program's basic method, and it invokes all others. 

What is args in Java?

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”.

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 use of String args within main ()?

String[] args in the main() method is used to receive input data when the Java program is run, like user commands or file names

Conclusion

In this blog, we discussed “public static void main string args”. We learnt the meaning of public, static, void, and main. We also discussed why String args[] is needed and saw what would happen if we remove it.

For further reading about Java, you can visit the following links -

Happy Learning!

Previous article
Difference‌ ‌between‌ ‌Java ‌and‌ ‌JavaScript
Next article
Java Lambda Expressions
Live masterclass