- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Why the main () method in Java is always static?
Java main() method is always static, so that compiler can call it without the creation of an object or before the creation of an object of the class.
- In any Java program, the main() method is the starting point from where compiler starts program execution. So, the compiler needs to call the main() method.
- If the main() is allowed to be non-static, then while calling the main() method JVM has to instantiate its class.
- While instantiating it has to call the constructor of that class, There will be ambiguity if the constructor of that class takes an argument.
- Static method of a class can be called by using the class name only without creating an object of a class.
- The main() method in Java must be declared public, static and void. If any of these are missing, the Java program will compile but a runtime error will be thrown.
Example
class Book { public static void getBookInfo() { //static method System.out.println("Welcome to TutorialsPoint Library"); } } public class Test { public static void main(String[] args) { //Call static method of Book class using class name only Book.getBookInfo(); } }
Output
Welcome to TutorialsPoint Library
- Related Articles
- Why main() method must be static in java?
- Why is the Main() method use in C# static?
- Can We declare main() method as Non-Static in java?
- How to execute a static block without main method in Java?
- Can we change the order of public static void main() to static public void main() in Java?
- Why the main method has to be in a java class?
- Is main method compulsory in Java?
- Why can't static method be abstract in Java?
- Why "this" keyword cannot be used in the main method of java class?
- Why can't we use the "super" keyword is in a static method in java?
- Java static method
- Why final variable doesn't require initialization in main method in java?
- What is the Eclipse keyboard shortcut for "public static void main(String[] args) " in Java?
- Can we override the main method in java?
- Can we overload the main method in Java?

Advertisements