Why main method is public static in Java (original) (raw)

The main method in Java is the first programming method a Java programmer knows when he starts learning Java programming language. have you ever thought about why the main method in Java is public, static, and void, of-course Yes, since most of us first learn C and C++ than we move to Java in our programming path we familiar with the main method but in Java main method is slightly different it doesn't return any value like in C it returns an int, the main method is public static and void Why?

In this post, we will try to find answers to these questions and have an idea of one of the most popular questions in Java why the main method is declared Static.

What is the main method in Java?

The main method in Java is the entry point for any core Java program. Remember we are not talking about Servlet, MIDlet, or any other container-managed Java program where life cycle methods are provided to control the execution.

In core Java program, execution starts from the main method when you type java main-class-name, JVM search for public static void main(String args[]) method in that class, and if it doesn't find that method it throws error NoSuchMethodError:main and terminates.

Signature of the main method in Java

The main method has to strictly follow its syntax; otherwise, JVM will not be able to locate it and your program will not run. Here is the exact signature of the main method

public static void main(String args[])

This signature is a classic signature and there from the start of Java but with the introduction of variable argument or varargs in Java5 you can also declare the main method in Java using varargs syntax as shown in the below example:

public static void main(String... args)

Remember varargs version of the java main method will only work in Java 1.5 or later versions.

Apart from the public, static and void, there are certain keywords like final, synchronized, and strictfp which are permitted in the signature of the java main method.

Why the main method is static in Java?

why main method is public static void in JavaNow come to the main point "Why the main method is static in Java", there are quite a few reasons around but here are few reasons which make sense to me:

1. Since the main method is static Java Virtual Machine can call it without creating an instance of a class that contains the main method.

2. Since C and C++ also have a similar main method which serves as an entry point for program execution, following that convention will only help Java.

3. If the main method were not declared static then JVM has to create an instance of the main Class and since the constructor can be overloaded and can have arguments there would not be any certain and consistent way for JVM to find the main method in Java.

4. Anything which is declared in class in Java comes under reference type and requires objects to be created before using them but the static method and static data are loaded into separate memory inside JVM called context which is created when a class is loaded. If the main method is static then it will be loaded in the JVM context and are available for execution.

Why the main method is public in Java

Java specifies several access modifiers e.g. private, protected, and public. Any method or variable which is declared public in Java can be accessed from outside of that class. Since the main method is public in

Java, JVM can easily access and execute it.

Why the main method is void in Java

Since the main method in Java is not supposed to return any value, it's made void which simply means main is not returning anything.

Summary:

1. The main method must be declared public, static and void in Java otherwise, JVM will not able to run Java program.

2. JVM throws NoSuchMethodException:main if it doesn't find the main method of predefined signature in class which is provided to Java command. E.g. if you run java Helloworld than JVM will search for public static void main String args[]) method in HelloWorld.class file.

3. The main method is an entry point for any Core Java program. Execution starts from the main method.

4. The main method is run by a special thread called "main" thread in Java. Your Java program will be running until your main thread is running or any non-daemon thread spawned from the main method is running.

5. When you see "Exception in Thread main” e.g.

Exception in Thread main: Java.lang.NullPointerException it means Exception is thrown inside main thread.

6. You can declare the main method using varargs syntax from Java 1.5 onwards e.g.

public static void main(String... args)

7. Apart from static, void, and public, you can use a final, synchronized and strictfp modifier in the signature of the main method in Java.

8. The main method in Java can be overloaded like any other method in Java but JVM will only call the main method with the specified signature specified above.

9. You can use the throws clause in the signature of the main method and can throw any checked or unchecked Exception.

10. A static initializer block is executed even before JVM calls the main method. They are executed when a Class is loaded into Memory by JVM.

Some tutorials you may like