News Stay informed about the latest enterprise technology news and product updates.

Anatomy of Java's main function

The entry point of a standalone Java application is the main method or function.

In the age of Servlets and JSPs, Java’s main function took a backseat to Servlets and JSPs that were initialized with a call to their init() method, and invoked through calls to their doGet() or doPost() methods. For enterprise web developers using JakartaEE APIs, there was little need for Java’s main function. But with cloud-native applications built with Spring Boot or the Eclipse Microprofile, developers are increasingly being exposed to the main method in Java, as it’s this function that typically kicks off the Tomcat, Jetty or WebSphere Liberty environment that hosts a microservice.

Here is the Java main function, or method, as it appears in a Spring Boot microservice.

Java’s main function

Java’s main method is composed of are six terms, including three reserved words, the main method name, a reference type, and a variable name:

  • public – Java’s main function requires a public access modified
  • static – Java’s main method is static, which means no instances need to be created beforehand to invoke it
  • void – Some programming languages return a zero or 1 to indicate the main method has run successfully to complete. Java’s main function is void, which means it does not return any value when it completes
  • main – When the JVM starts a standalone application, the main method is the function that gets invoked
  • String[ ] – An array of configuration parameters to be used by the application can be passed into the main function as arguments
  • args – The configuration parameters passed into the main function in Java are typically named args.

Java main function syntax

When you pull all of these details together, Java’s main method reads as follows:

public static void main(String[] args) { }

It’s worth noting that Java allows the square brackets to be placed on either the reference type or the variable name when an array is declared, so both of these derivations are valid:

public static void main(String[] args) { }
public static void main(String args[]) { }

Similarly, the convention is to name the array args, although any valid variable name can be used. So these derivations of Java’s main function are also valid:

public static void main(String[] arguments) { }
public static void main(String configuration[]) { }

On Java certification exams, it’s not unusual for the test authors to attempt to confuse test-takers by changing the name of the array, or moving the square brackets from the left to the right. But in practice, the the square brackets are typically placed on the String, and the variable is typically named args.

Java main method example

Source code for a full class file that contains a Java main method would look as follows:

package com.mcnz.main.function;
public class MyMainFunction {
  /* Java main function example */
  public static void main(String[] args) {
    System.out.println("Hello from the Java Main Function!");
  }
}

It’s also worth mentioning that Java developers don’t typically use the word function. The term ‘main method’ is the most common vernacular used when referring to the entry point of a Java application.

 

 

 

SearchAppArchitecture

SearchSoftwareQuality

SearchCloudComputing

SearchSecurity

SearchAWS

Close