The Main method is a special method that is used to execute a program. When you run a java program in the command prompt then it calls main method. In this article, I am going to tell you why main method is public static void and why it is important to understand main method.
Main method in java is the first entry point of a class. It is where you will start the execution of a class. This main method is public static void and you can’t change the main method. The main method in java is the entry point of the class and it’s always present in the class file. The main method in java is the first method that executes when you run a java program. If you are a beginner in java programming then you may not know the concept of main method in java. If you want to understand the main method in java, then you must read this article. Here are the 5 important reasons why main method in java is public static void.
What is the Main method?
The Main method is a method that is used to execute a program. The method contains the code that is used to run the program. The main method is a method which will be called when the program starts. The main method is required to be declared at the start of the code. This method will be executed when the application starts.
For example:
public class Main
{
public static void main(String args[])
{
// your code here
}
}
Main Method with Parameters: This is the most common type of the main method. Here you need to specify the arguments or the parameters.
public class Main
{
public static void main(String args[])
{
// your code here
}
}
Main Method without Parameters – If you don’t provide any arguments or parameters then it is known as the main method without parameters.
public class Main
{
public static void main()
{
// your code here
}
}
Main Method without Parameter and Argument: If you don’t provide any arguments or parameters then it is known as the main method without parameters and arguments.
public class Main
{
public static void main()
{
// your code here
}
}
Why Main method is public static void?
When you run a program in the command prompt, then it calls main method of the class. So, the main method is the first thing that will be called when you run the program. That means main method is the first thing to be executed. This is the reason that the main method is a static method.
A static method is a method that can be accessed without creating an object. When you create an object, it creates a new memory space, so it is impossible to access the static method without an object. So, main method is a static method and it can be accessed without an object.
Why public, private or protected?
The public method can be accessed from outside, the private method can be accessed only inside the class and the protected method can be accessible both within and outside the class.
Conclusion:
I hope this article will help you to understand the importance of the main method and how it is a static method. If you have any questions regarding this article, then you can leave a comment below.