public static void main(String[] args) – Java main method

In Java programs, the point from where the program starts its execution or simply the entry point of Java programs is the main() method.

Why is the main method so important?

The Java compiler or JVM looks for the main method when it starts executing a Java program. The signature of the main method needs to be in a specific way for the JVM to recognize that method as its entry point. If we change the signature of the method, the program compiles but does not execute.

The execution of Java program, the java.exe is called. The Java.exe inturn makes Java Native Interface or JNI calls, and they load the JVM. The java.exe parses the command line, generates a new String array, and invokes the main() method. A daemon thread is attached to the main method, and this thread gets destroyed only when the Java program stops execution.

Java Native Interface or JNI:[TBD]

While you can write applications entirely in Java, there are situations where Java alone does not meet the needs of your application. Programmers use the JNI to write Java native methods to handle those situations when an application cannot be written entirely in Java.

The following examples illustrate when you need to use Java native methods:

  • The standard Java class library does not support the platform-dependent features needed by the application.
  • You already have a library written in another language, and wish to make it accessible to Java code through the JNI.
  • You want to implement a small portion of time-critical code in a lower-level language such as assembly.

By programming through the JNI, you can use native methods to:

  • Create, inspect, and update Java objects (including arrays and strings).
  • Call Java methods.
  • Catch and throw exceptions.
  • Load classes and obtain class information.
  • Perform runtime type checking.

You can also use the JNI with the Invocation API to enable an arbitrary native application to embed the Java VM. This allows programmers to easily make their existing applications Java-enabled without having to link with the VM source code.

Daemon thread:

Daemon thread in Java is a low-priority thread that runs in the background to perform tasks such as garbage collection. Daemon thread in Java is also a service provider thread that provides services to the user thread. 

There are mainly three ways in which we can write the main method in Java. They are as follows:

Java main method variants:

public static void main(String[] args)
public static void main(String... args)
static public void main(String args[])

Explanation of the Keywords:

The keywords in the method: public static void main(String args[]) are as follows:


Public: Public is an access specifier. Marking a method as public makes it visible to all methods across all packages. We need to mark the main() method as public otherwise, it is not visible to the JVM.


Static: The JVM invokes the main method without creating objects and hence the main method needs to be marked static.(It is a keyword that is when associated with a method, making it a class-related method. The main() method is static so that JVM can invoke it without instantiating the class. This also saves the unnecessary wastage of memory which would have been used by the object declared only for calling the main() method by the JVM.)


Void: Since the main method does not need to return anything, the main method is marked void. If we define a return type for the main method, the program execution fails.


main(): This is the default signature as defined by the JVM.


Strings args[ ]: These are arguments passed to the main method so that users can give inputs while running the program in the form of an array of Strings.

What happens if the main() method is written without String args[]?

The program will compile, but not run, because JVM will not recognize the main() method. Remember JVM always looks for the main() method with a string type array as a parameter.

Execution Process:

First, JVM executes the static block, then it executes static methods, and then it creates the object needed by the program. Finally, it executes the instance methods. JVM executes a static block on the highest priority basis. It means JVM first goes to static block even before it looks for the main() method in the program.

Reference:

https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/intro.html#:~:text=The%20JNI%20is%20a%20native,%2C%20C%2B%2B%2C%20and%20assembly.

https://www.ibm.com/docs/en/sdk-java-technology/7?topic=components-java-native-interface-jni

https://www.geeksforgeeks.org/daemon-thread-java/#:~:text=Daemon%20thread%20in%20Java%20is,services%20to%20the%20user%20thread.

https://www.javatpoint.com/daemon-thread

https://www3.ntu.edu.sg/home/ehchua/programming/java/javanativeinterface.html

https://www.javatpoint.com/java-main-method

https://java2blog.com/public-static-void-main-string-args-java-main-method/

https://www.geeksforgeeks.org/java-main-method-public-static-void-main-string-args/

Leave a comment

Design a site like this with WordPress.com
Get started