Java's main function explained with examples

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 these technologies  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.

Java main function

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

Java’s main function

Java’s main function

Java’s main method is composed of six terms — 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 signature

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[]) { }

Alternate main function syntax

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 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!");
  }
}

Why is the main method static?

The static keyword often confuses junior Java developers. A static variable is one that is not attached to an instance, and a static method is one that does not require an instance to be invoked. Instances can only be created after a Java application has started. If the main method was not static, it would require code to have already run in order for it to be invoked. The static keyword on the main method allows this function to be used as the entry point for an application, before any other Java code has run, and before any Java instances have been created.

JavaScript main function

Java and JavaScript are often confused with each other and serve very different purposes. JavaScript is embedded within a web page. Any code placed within an HTML script tag in a webpage will be executed. There is no need to code a JavaScript main function when doing web page development.

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.

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close