Simple Hello Programs in java

 

What is the main method in Java?

Main method in Java is 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 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; other wise 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 classic signature and there from start of Java but with introduction of  variable argument or varargs in Java5 you can also declare the main method in Java using varargs syntax as shown in below example:

public static void main(String… args)

Remember varargs version of java main method will only work in Java 1.5 or later version. Apart from public, static and void, there are certain keywords like final, synchronized and strictfp which are permitted in the signature of java main method.

Why main method is static in Java

 

Now 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 any instance of a class which contains the main method.
  2. Since C and C++ also have similar main method which serves as entry point for program execution, following that convention will only help Java.
  3. If main method were not declared static than JVM has to create instance of main Class and since constructor can be overloaded and can have arguments there would not be any certain and consistent way forJVM to find main method in Java.
  1. Anything which is declared in class in Java comes under reference type and requires object to be created before using them but static method and static data are loaded into separate memory inside JVM called context which is created when a class is loaded. If main method is static than it will be loaded in JVM context and are available to execution.

Why 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 accessible 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 declaredpublic, static and void in Javaotherwise, JVM will not able to run Java program.
  2. JVM throwsNoSuchMethodException:mainif 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.

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

public static void main(String… args)

  1. Apart from static, void and public, you can use a final, synchronized and strictfp modifier in the signature of the main method in Java.
  2. The main method in Java can be overloaded like any other method in Java but JVM will only call the main method with specified signature specified above.

 

  1. You can use throws clause in the signature of the main method and can throw any checked or unchecked Exception.
  2. A static initializer blockis executed even before JVM calls the main method. They are executed when a Class is loaded into Memory by JVM.

 

Why main() in java is declared as public static void main? What if the main method is declared as private?

Public – main method is called by JVM to run the method which is outside the scope of project therefore the access specifier has to be public to permit call from anywhere outside the application static – When the JVM makes are call to the main method there is not object existing for the class being called therefore it has to have static method to allow invocation from class. void – Java is platform independent language therefore if it will return some value then the value may mean different to different platforms so unlike C it can not assume a behavior of returning value to the operating system. If main method is declared as private then – Program will compile properly but at run-time it will give “Main method not public.” error.