What is Public Static Void Main in Java?
The public static void main() is the most important java method. The compiler starts executing the java program from the main method. Hope you remember when you wrote your's first java program.
Now, the question is what are these prefixes i.e public, static, and void in the main() method? What are the uses of these terms in these questions? Can we avoid prefixes? Let's dive deep, and find the in-depth knowledge of these terms.
Why Do We Use Public Static Void Main in Java?
The main() method is used to execute the java program, whether it is a small program or a large codebase program, Now the question is can we run the java program without implementing the main() method? The answer is no, every java program must have a main() method to execute the program.
Let's understand the execution life cycle of the java program.
During the execution of any java program, the JVM(java virtual machine) search the main() method of the java program to execute that's why the main() method is marked by public specifier because the JVM externally calls the main() method for executing, and the scope of public specifier allows to access it. Refer to this article to understand the scope of the different modifiers. Access Modifiers in java.
Now the question arises, what will happen if we try to use another specifier in the main() method in the place of the public. Let's see using an example.
Output:
We can see clearly the java compiler will throw an error. if we try to use another specifier instead of the public in the main() method.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Why main() Method in Java Must be Static?
Let's understand what is the use case of static and the importance of the static method in the main() method.
We know very well, that we need to create an object of the class to access its method, and creating an object requires extra space in the memory, and we know very well we can access the static method of the class without creating an object of the class. Refer to this article Static member.
Let's understand them without the help of some examples.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Examples of Public Static Void Main in Java
Avoiding the Static keyword in the Main Method.
Let's understand what will happen if we avoid the static prefix in the main() method.
Output:
Turn Learning into Career Growth
Changing the Position of the Public and Static keyword in the Main Method.
This is the most popular interview question that is what will happen if we interchange the position of the public and static keyword in the main() method.
Let's understand this using an example. In the example, we interchanged the position of the static and the public keyword.
Output:
We can interchange the position of the static and the public keywords in the main() method, but changing the position with other keywords like the void in the main() method will cause an error.
Parameter of the main() Method.
The main() method takes a String array[] as an argument that helps to take the value from the user.
Let's understand this using an example. In the below example, we are printing the length of the String array length, and the values of the first two indexes.
Output:
Learn more about Static Method in Java
Learn more about the static method in java Static method
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Conclusion
- JVM executes the main() method of the class.
- The main() method must be of static and public type.
- We cannot avoid the main() method if we want to execute the java program.