I am not sure what this means, whenever before you write a code, people say this
public static void main(String[] args) {
What does that mean?
I am not sure what this means, whenever before you write a code, people say this
public static void main(String[] args) {
What does that mean?
In Java your main method must always be:
The program execution starts with
main()
function, hence themain()
function.It must be
public
so that it is accessible to the outside environment.The
main()
method is always static because, as you know that the program execution starts atmain()
method and there is no instance of the class containingmain()
method is initiated. Hence as the static method can run without need of any instance it is declared of static.Java is platform independent, hence you may try to compile the java file on one system and try to execute the class file on another. Each machines' bit architecture may be different hence the return type of the main function must always be
main()
.Hope this helps.
Public
= This method is visible to all other classes.static
= This method doesn't need an instance to be ran.void
= This method doesn't return anything.main()
= Main method (First method to run).String[]
= Array of strings.args
= Array name.According to the Java language specification, a Java program's execution starts from main() method. A main() method should follow the specific syntax, it can be explained as:
public
- Access specifier, shows thatmain()
is accessible to all other classes.void
- return type,main()
returns nothing.String args[]
- arguments to themain()
method, which should an array of type string.static
- Access modifier. A main method should always be static, because the `main()' method can be called without creating an instance of the class.Let us assume, we are executing a Helloworld java program.
While executing the program, we use the command
Internally, this command is converted into
By making
main()
method static, JVM calls themain()
method without creating an object first.Here is a little bit detailed explanation on why main method is declared as
Main method is the entry point of a Java program for the Java Virtual Machine(JVM). Let's say we have a class called
Sample
This program will be executed after compilation as
java Test
. Thejava
command will start the JVM and it will load ourTest.java
class into the memory. As main is the entry point for our program, JVM will search for main method which is declared aspublic
,static
andvoid
.main()
must be declaredpublic
because as we know it is invoked by JVM whenever the program execution starts and JVM does not belong to our program package.In order to access main outside the package we have to declare it as
public
. If we declare it as anything other thanpublic
it shows aRun time Error
but notCompilation time error
.main()
must be declared as static because JVM does not know how to create an object of a class, so it needs a standard way to access the main method which is possible by declaringmain()
asstatic
.If a method is declared as
static
then we can call that method outside the class without creating an object using the syntaxClassName.methodName();
.So in this way JVM can call our main method as
<ClassName>.<Main-Method>
main()
must be declared void because JVM is not expecting any value frommain()
. So,it must be declared asvoid
.If other return type is provided,the it is a
RunTimeError
i.e.,NoSuchMethodFoundError
.main(
) must have String arguments as arrays because JVM calls main method by passing command line argument. As they are stored in string array object it is passed as an argument tomain()
.public
--> Access specifier. Any other class can access this method.static
--> The method is bound to the class, not to an instance of the class.void
--> Return type. The method doesn't return anything.main(String[] args)
--> method name ismain()
. It takes an array of String's as argument. TheString[]
args are command line arguments.Note: The
main()
method defined above is the entry point of a program, if you change the signature, then your program might not run.