Why main() method is static in Java?
It is a very common Java question that baffles new learners as well as experienced programmers. Very few people know the correct answer of this question. In this post, I will share the real concept behind it.
Everything that we define in a class is called a member of the class. Members of a class can be of two types:
- Instance Member
- Class Member
A member which describe some property or behavior of an object of the defined class, is called an Instance Member. E.g. let India be a class then all the Indians are its objects. Each Indian has some properties such as name, age, qualification etc. Value of these properties differ from Indian to Indian i.e. object to object. These properties are examples of Instance members.
A member which describe some property or behavior of the defined class as a whole, is called a Class Member. E.g. India as a class also has some properties such as currency, capital, flag etc. Value of these properties is common for all the Indians i.e all the objects. These properties are examples of class members.
How are instance and class members differentiated?
By default each member of a class is assumed to be Instance Member. To define class members static keyword is used. E.g. Following class contains both instance as well as class members. Each class member is qualified by static keyword.
Why main() method is static?
The main() method of a class denotes the entry point of the class. It is like opening a school. Once the school is opened, employees, teachers and students arrive as objects to manifest their behavior. As class members are associated to the class, no object is required to access them. By default each member is associated to object hence to associate the main() method to the class, it is qualified by static keyword.