Why main method is static in Java
The method serves as the entry point for Java programmes or simply the point from which the programme begins to run. As a result, it is one of the most crucial Java methods, making it essential to comprehend it thoroughly.
When a Java application is being run, the JVM or Java compiler searches for the primary method. The main method's signature must be in a specified format for the JVM to recognise it as the entry point. The software builds but does not run if we alter the method's signature.
The java.exe programme is used to run the Java programme. The JVM is then loaded by Java.exe, making JNI calls. Invoking the primary () method after parsing the command line and creating a new String array. When the Java programme stops running, the daemon thread tied to the main function is removed.
Syntax: Typically used when defining the main() method
A sample java program for Hello world in java.
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Output:
Hello, World!
Explanation of output: The public static void main statement's words are all notifed by the JVM.
- public
- static
- void
- main
public: It is an access modifier that specifies who can access the method and where they can do so. The main() method becomes globally accessible when it is made public. Since it is not a part of the current class, it is made public so that JVM can call it from outside the class.
// Java programme illustrating the
// use of any additional access modifier
// exclude the public
class HelloWorld {
private static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Output:
Error: A javafx application class must extend javafx.application to prevent the main method from failing to comply with public static void main's criteria (String[] args). Application
Static:
When used in conjunction with a method, it is a keyword that designates the technique as class-related. The static nature of the main() method allows JVM to call it without first creating the class. Additionally, this prevents the needless waste of memory that would have been incurred had the object been defined only to have the JVM invoke the main() method.
// Java application to show the
// When we don't use them, an error happens.
// The main() method uses the static keyword.
class HelloWorld {
private static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Explanation of output: The main() method in the class test should be public static void main instead of not being static (String[] args)
void:
A keyword is employed to indicate that a method returns nothing. The main() method's return type is void because it doesn't produce a result. The main() method's termination also causes the Java programme to end. As a result, returning from the main() method is useless because the JVM cannot use its return value.
class HelloWorld {
private static void main(String[] args) {
System.out.println("Hello, World!");
return 1;
}
}
Explanation of output: Either a JavaFX application class must extend or the main function must be a public static void main(String[] args). javafx.application. The main form is not found in the category. Application
Main:
The Java main method has that name. When the Java programme is first run, the JVM searches for this identifier. It’s Not at all a keyword.
class HelloWorld {
public static void myMain(String[] args) {
System.out.println("Hello, World!");
}
}
Explanation of output:
In the event that the main method cannot be found in the category, the JavaFX application class must extend javafx.application. The main method might also be made public static void main as an alternative (String[] args). Application
String[]args:
It contains Java command-line arguments and is an array of type java.lang. Class for strings Although args is the String array's name. The user is free to substitute any other name in this case.
Numbers.java
class Numbers
{
public static void main(String[] args)
{
for (String elem : args)
System.out.println(elem);
}
}
Output:
5
6
7
In addition to the above-mentioned main function signature, java also supports calling the main function with public static void main(String args[]) and public static void main(String args). If the formal parameter of the main method matches an array of Strings, the main method is invoked.
The justifications for making the main() function static:
- Users already know that when the JVM starts, there are no objects of a class. Because of this, we are unable to call a method without first generating an instance of its class. The main() method is made static to allow the JVM to load the class into main memory.
- The entry point of any Java programme is the main() function. The main() method is necessary since this is where the compiler initiates programme execution.
3.If it is allowed for the main() function to be non-static, the JVM must instantiate the class. - By using the class name alone, JVM can easily use the static methods without generating an instance of the class.
- As already mentioned, the main() method needs to be static, public, and void return. If the method doesn't return anything or if we don't specify it as public and static, it will surely throw an error.