Java main method
Anyone who is familiar with java must be knowing main
method. Whenever a class is executed using java
command(or through an IDE), java main
method is called first.
In other words, main
method is the entry point of a class or an application having multiple classes.
This post focuses on the detailed explanation of signature of this method and it will also answer some of the most commonly framed interview questions on public static void main
.
Signature of main method is
public static void main(String[] args)
. It consists of 4 keywords where
public
is the access specifier,static
is an access modifier,void
is the return type andmain
is the name of method.
All of these keywords of main
method are explained in detail below.
public
This keyword makes main
method exposed to outside this class.
Since this method is always called from outside a class, it should be declared as public
else it will not be visible external to the class.
If main
method is preceded with any other access specifier(private
or protected
), then it would not be visible to JVM and an error will be thrown.
Example,
public class MainNotPublic { // main method has protected access protected static void main(String[] args) { System.out.print("Main is protected"); } }
Above class declares main
method as protected
and gets the following message when executed.
Error: Main method not found in class MainNotPublic , please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
This is a common interview question based on main
method
“Why main is public in java”
static
Applying static keyword to a field or method binds it to the class.
This means that in order to access a static field or method, instance of the class is not required and it can be directly accessed using class name.
Also, there is only one copy of a static
field or method and it is shared by all instances of the class.
When a class is executed, there is no instance of it. Hence, in order to call a method without an instance, it should be marked as static
.
This is also an answer to an important interview question
“why main method is static in java”.
What should happen when main
is not declared static
as in the class below
public class MainNotStatic { // main method is non-static public void main(String[] args) { System.out.print("Main is not static"); } }
You are right, an error is received when this class is executed.
Error: Main method is not static in class MainNotStatic, please define the main method as:
public static void main(String[] args)
void
Return type of a method indicating that the method does not return any value.
In java, main
method does not return any value, hence it should be preceded with void
.
If the main
method is given any other return type, then an Error will be thrown at execution time.
Example,
Suppose a class has following main
method.
public class MainWithReturn { // main method returning an integer public static int main(String[] args) { return 0; } }
This class compiles fine but when executed shows the following.
Error: Main method must return a value of type void in class MainWithReturn, please
define the main method as:
public static void main(String[] args)
java
command.When executing from an IDE such as eclipse, you will not see an option to run the class as a Java program.
String[] args
main
method in java accepts an array of String arguments.
This array argument is used for holding parameters sent to the class.
Any command line parameters supplied while executing a class are automatically populated in this array. There is no limit to the length of this array.
This also means that you can pass any number of arguments while running a java program. Example,
If a class is executed as java MyClass John 20 Student
then the array argument of main
method of MyClass
will have below elements
args[0] = John
args[1] = 20
args[2] = Student
Can I create a method with name main in a class ?
Off course you can create a method which has name as main but it will be considered as an overloaded method and will be treated as just like any other method which has its name as main
.
Look at the below example,
public class SecureImp{ public static void main(String[] args) { System.out.println("Entry method of class"); // create an object of this class SecureImp obj = new SecureImp(); // call method obj.main(""); } /** * Another method with name as main **/ public void main(String args) { System.out.println("Another main method"); } }
When the class is executed, main
method runs which creates an object of this class and calls another method which also has name as main.
Output is :
Entry method of class
Another main method
Note that if argument count and types of both methods will be same, then the class will not compile.
Let’s tweak in
String[] args
can also be written asString args[]
.- It is not necessary to name the argument String array to args. It can be any name as per java variable naming rules.
public
andstatic
are interchangeable. This means thatmain
method can also be declared asstatic public void main(String[] a)
.- Any parameters passed to a class are automatically populated in the
String
array parameter of themain
method.
Hope this post was helpful to you. Click the clap below.