Java :public static void main(String[] args)
The main() method is a special method in Java Programming that serves as the externally exposed entrance point by which a Java program can be run. To compile a Java program, you doesn't really need a main() method in your program. But, while execution JVM ( Java Virtual Machine ) searches for the main() method and starts executing from it.
The main() method must be public, it means that you can call this method from outside of the class you are currently in. Since it's static method , there doesn't need to be an instantiation of the class. It must not return any value, and it must accept a String array as a parameter.
public static void main(String[] args){
}
- The modifiers public and static can be written in either order (static public or public static), but the convention is to use public static as shown above.
- You can define a main() method with any access modifier or with/without static keyword, but then it is not a valid main() method, as the main method which the JVM uses as an entry-point should be defined as such.
- You can name the argument anything you want, but most developers choose "args" or "argv".
public class sample {
public void msg(){
System.out.println("without main()...");
}
}
output
Error: Main method not found in class sample, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Let's split it and understand one by one.
public
It is an access specifier that means main() method is accessible globally available . This is necessary because this method is being called by the Java Runtime Environment (JRE) which is not located in your current class. It is important to note that if you make main() method non-public then it's not allowed to be executed by any program, there are some access restrictions applied.
class sample {
static void main(String[] args) //without public access specifier
{
System.out.println("Without public...");
}
}
output
Error: Main method not found in class sample, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
static
The main() method in Java must be static, because they can then be invoked by the runtime engine without having to instantiate any objects then the code in the body of main() method will do the rest. The main() method should be static because otherwise there would be ambiguity : which constructor should be called? If the main() is allowed to be non-static, then while calling the main() method JVM has to instantiate its class. While instantiating it has to call the constructor of that class, There will be ambiguity if the constructor of that class takes an argument.
class sample {
public void main(String[] args) //without static..
{
System.out.println("Without static...");
}
}
output
Error: Main method is not static in class sample, please define the main method
as:
public static void main(String[] args)
void
Java is a platform independent programming language and if it will return some value then the value may mean different things to different platforms. The "void" is a return type i.e it does not return any value. When the main() method terminates, the java program terminates too. Hence, it doesn't make any sense to return from main() method. If the main() method spawns new threads, then these threads can keep program running. The return type of main doesn't make much sense at this point. If you try to return something from the main method, it will give compilation error as an unexpected return value.
class sample {
public static int main(String[] args) //int instead of void
{
System.out.println("Without void...");
}
}
output
sample.java:6: error: missing return statement
}
1 error
![Understanding public static void main(String[] args) in Java](img/main-method.png)
main()
It's just the name of method or a function name. This name is fixed and as it's called by the JVM as entry point for an application. It's not a keyword.
class sample {
public static void mian(String[] args) //mian() instead id main() method
{
System.out.println("Without main()...");
}
}
output
Error: Main method not found in class sample, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
String args[]
These are the arguments of type String that your Java application accepts when you run it. Java main() method accepts only string type of argument and stores it in a string array. It is a collection of Strings , separated by a space, which can be typed into the program on the terminal. As the Java language allows the brackets [] to be positioned after the type or the variable (the first is generally preferred)
public static void main( String args[] ) // valid but usually non recommended
If you wanted to output the contents of args , you can just loop through them like this...
class sample {
public static void main(String[] args)
{
for(int i = 0; i < args.length; i++) {
System.out.println(args[i]);
}
}
}
Run the java program with arguments like the following:
java sample one two three
output
one
two
three
Related Topics