Explain the significance of using each keyword in “public static void main(String args[])”
In Java programs, the point from where the program starts its execution or simply the entry
point of Java programs is the main() method.
The keyword public static void main is the means by which we create a main method within
the Java application.. It's the core method of the program and calls all others. It can't return
values and accepts parameters for complex command
command-line processing.
Every word in the public static void main statement has got a meaning to the JVM. The Java
compiler or JVM looks for the main method when it starts executing a Java program. The
signature of the main method needs to be in a specific way for the JVM to recognize that
method as its entry point. We can change the signature of the method, then the program
compiles but does not execute.
1. Public
It is an Access modifier,, which specifies from where and who can access the method. Making
the main() method public makes it globally ava
available.
ilable. It is made public so that JVM can invoke
it from outside the class as it is not present in the current class.
2. Static
It is a keyword that is when associated with a method, making it a class-related
class method.
The main() method is static so that JVM can invoke it without instantiating the class. This also
saves the unnecessary wastage of memory which would have been used by the object declared
only for calling the main() method by the JVM.
3. Void
It is a keyword and is used to specify that a method doesn’t return anything. As
the main() method doesn’t return anything, its return type is void. As soon as
the main() method terminates, the java program terminates too. Hence, it doesn’t make any
sense to return from the main() method as JVM can’t do anything with the return value of it.
4. main
It is the name of the Java main method. It is the identifier that the JVM looks for as the starting
point of the java program. It’s not a keyword.
5. String[] args
It stores Java command-line arguments and is an array of type [Link] class. Here, the
name of the String array is args but it is not fixed and the user can use any name in place of it.
So, here public is used as an access modifier for a main method. Static is used so that it can
directly load in memory without creating any instance of that class. Void is used because it does
not return any value and main is the entry point of program.
NOTE-1: Apart from the above-mentioned signature of main, we could use public static void
main(String args[]) or public static void main(String… args) to call the main function in
Java. The main method is called if its formal parameter matches that of an array of Strings.
NOTE-2: Java does not return int implicitly, even if we declare the return type of main as int.
We will get a compile-time error.