Java Lab Documentation

The main Method

Category:Java Basics

The main method in Java is arguably the most important method – it is the entry-point into any Java program. The syntax is the same for any Java program. The JVM (Java Virtual Machine) executes Java byte code, and the main method must have specific syntax for the JVM to identify it and execute its contents.

public static void main(String[] args)

public: The method needs to be public for the JVM to identify it.

static: static in this context means that there will be only one type of this method and that it will be shared. When we call the main method, it does not require a new instantiated object – it can be called without the creation of a new object.

void: There is nothing returned from the main method.

main: The general signature of the main method that is identified by the JVM to execute any program from start to finish.

String[] args: The main method's argument (or input parameter) is an array of type String. This allows the method to accept command line arguments, which are stored as Strings in this variable. The name args can be changed.

Examples

This method will be identified by the JVM and will execute accordingly assuming there are no other errors.

public static void main(String [] args) {
   System.out.println("Main Method");
}

Syntax

public static void main(String[] args)