Why main() method is static in Java?

In this post, you will learn:

  • Why main() method is static?
  • What is instance member?
  • What is class member?

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

Instance 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.

Class Member

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.

public class AB
{
//object properties
private int a,b;
//class property
private static int counter;

/*
Each object of this class will have individual copies of a & b but there will be only one counter which will be shared by all the objects.
*/
}

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.

Its time to recap the main concepts:

  1. In a class members can be of two types: Instance Members and Class Members.
  2. Instance members describe properties or behavior of individual objects.
  3. Class members describe collective properties or behavior of all the objects of a class.
  4. main() method denotes the execution of a class i.e. it is a class member hence it is qualified with static keyword.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

2 thoughts on “Why main() method is static in Java?

    1. static keyword can only be used with members of a class not with the class i.e. a class can’t be made static unless it is nested within another class as a member.

Leave a Reply

Your email address will not be published. Required fields are marked *