Why is the main method static in java?

To understand this first you need to know why do we create static methods and how do we call them learn about static methods at :

Main method is declared static because static methods do not need any instance to get themselves called. Java is Object Oriented language so it is necessary to declare the  main method as static because their are some conditions in which we don’t want to create the object for example if we want to do simple programming without using Concepts of OOP then their is no way to call the main method but by declaring it static. The code below will help you understand.

  public class MyClass{

public void main(String arguments[]){

System.out.println(“codeheaven.wordpress.com”);

}

}

The above code generates an error: main method is not static please define the main method as:
   public static void main(String[] args)
But when the main is made static this code runs fine and gives the output: codeheaven.wordpress.com

          If you find my answer intresting and easy to understand invite your friends to read this and Don’t Forget to Like and give your feedback and suggesions 🙂

Advertisements