Why main method is static in Java?

One of the well-establish and most common programming languages is Java. Java came into existence in 1995. And to that date, C and C++ languages consistently widespread in the world of computer science. The ambition to make Java is to build familiarity, security, and portability of data. The main reason to make Java language is to make application programs independent of the hardware system to which the program is running.

JVM (Java virtual machine) comes under the category of Abstract machine. Whenever Java bytecode is running, JVM cataloging provides the runtime time environment for the Java bytecode. The language implicates WORA (Write once run anywhere). The Java program, which is written on one device, can be executed on any device that supports JVM. Considering the program security and portability, the word static and final are in use in Java programming.

In this article, we will answer why main method is static in Java.

What are static variables and static functions?

What is the term “static” Signify? Whenever there is a static keyword besides a variable or function, it belongs to the class itself. A static variable is initialized at the commencing of the program, and only a one-time declaration is required. Only one copy of the variable is shared among all the instances of the class.

Static members are shared with all instances of the classes. Whenever a static keyword is applied, it signifies it doesn’t require any instance to access the variable and function, it can be accessed directly by the class itself.

//static variable declaration
static int a;

 

//static function declaration
static void fun(){
…..
}

 

//static function call
class company:
{
static void fun(){

}

}

company.fun() // Class name can directly access the static function. No need to declare an instance to call the functions.

What is the use of main() in the program?

Main() is the first point where from compiler start executing the program. With the execution of the main() function in the program, the RAM is allocated to the task. In Java, the main() is the entry point for JVM in the program. JVM introduces the Java program by requesting the main() function.

Why static main()?

As the main() method is the starting point for the compiler to execute the program, therefore declaring the main() static is necessary for the following ways.

  • The static main() method makes a path clear for JVM to call the main() function for proceeding with the program. Or else we have to specify the entry point for each Java program development to make JVM execute the program.
  •  While there is a declaration of instance in the program, it has to call the constructor of the class. There will be a rise in ambiguity(which constructor should be call) in the program when the constructor of the particular class takes the argument. That’s why the main should be static.
  • The main() method should be static because it is convenient for JDK (java development kit). Consider a case when static is not mandatory for main(). It will be difficult for various IDEs (integrated development environment) to identify the launchable classes in the project. Therefore, it is the convention to making the entry method main() static.

public static void main(String[] args) 

  • The main() method must contain the public, static, and return types. If any of the following terms are missing, the compilation would take place. But at runtime, the JVM searches for the public, return type static, and array string as an argument if it would not found, then the error is shown at the runtime.

Leave a Reply

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