The main() method

When we execute a Java program, the JVM (Java Virtual Machine) will look for the main() method to start a Java process for our program and then the JVM calls the underlying system in which is running to allocate memory and CPU time, an so on, until the program terminates.

Creating a main() method

The main() method is a special method in Java that serves as the entry point for a Java program. It is the starting point from where the execution of the program begins. Every Java program must have a main method in at least one class.

The main method has the following signature:

public static void main(String[] args)

Let’s break down the components of the main method:

  • public: It is the access modifier, indicating that the main method can be accessed from anywhere in the program.
  • static: It is a keyword that means the method belongs to the class itself rather than an instance of the class.
  • void: It specifies that the main method does not return any value.
  • main: It is the name of the method. The Java Virtual Machine (JVM) looks for this method as the entry point of the program.
  • (String[] args): It is the parameter passed to the main method. It represents an array of strings that can be used to pass command-line arguments to the program. Also is possible to have a String varargs instead of an array of Strings. The next are examples of the different ways that the parameter args can be in the main() method:

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

Inside the main method, we will write the code that we want the program execute. It typically contains the primary logic and control flow of your application. A main() method looks like this:

/**
* This Wood class is to demostrate the how the main method looks
*/
public class Wood {
/**
* This is the main method for the Wood program
*/
public static void main(String[] arg){
System.out.println("Starting the Wood program!!!");
}
}
view raw Wood.java hosted with ❤ by GitHub

In this example the code doesn’t do anything besides to print in console the message: "Starting the Wood program!!!".

Running a Java Program

To run a Java program we use the command java in a terminal and the name of the class, of course we need to compile first the Java Program with javac. The previous class can be executed with the next commands (first we will compile with javac and then run with java).

javac Wood.java
java Wood

What about if we need to send data to the program to run, like a file or extra information, well here comes to play the args parameter in the main() method that is use to provide or send data to our program. The data is handled as an array of strings, this means that even if we send numbers (integers, longs, doubles, etc) they still be Strings. This data is send when we execute the program using the command java, let’s see an example:

javac Wood.java
java Wood Squirrels Wood

When we execute the previous command, happens this: first javac compiles the class or file Wood.java, if there are not any error the the next to be executed id the java command, this command that indicates that the Wood program must be executed with the aditional arguments (or data) of “Squirrels” and “Wood“, this parameters should be separated by a white space. So, then the JVM will look for the main() method in the Wood class, and the arguments Squirrels” and “Wood” are passed to the String[] args parameter. Rememeber that this arguments are handled as an array of String.

Now let’s add some lines to our Wood class:

/**
* This Wood class is to demostrate the how the main method looks
*/
public class Wood {
/**
* This is the main method for the Wood program
*/
public static void main(String[] arg){
System.out.println("Starting the Wood program!!!");
//The first parameter is loated in the position 0, and so on if we have more arguments
System.out.println(arg[0]);
//The second parameter is located in the position 1
System.out.println(arg[1]);
//If we have more parameters this will continue in the next position
}
}
view raw Wood.java hosted with ❤ by GitHub

If we run this class, we will get the next message in the console:

javac Wood.java
java
Wood Squirrels Wood
Starting the Wood program!!!
Squirrels
Wood

In this case, we have only two arguments, this means that args is an array of length of 2, this means that the first argument in this case “Squirrels” is the args[0] and the second argument “Wood” is in the args[1]. If we have more parameters they will be in the next positions of the args array.

What happend if in Wood program, we send only one parameter instead of two?

javac Wood.java
java Wood Squirrels
Starting the Wood program!!!
java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
 at Zoo.main(Zoo.java:4)

We will get an Exception, because we only send 1 argument and we need two arguments to run the Wood program. We need to be careful, with the argument list, because if we don’t send the correct number of arguments the program will not run.

Tips for Running a Java Program

Sometimes, we need to pass arguments that have more than 1 word for example the arguments “Hello Squirrel from the” and “Wood”. So, to do it we need to surround the words that will be one argument with the double quote “”. In this case the first argument will be “Hello Squirrel from the” and the second argument will be “Wood”. If we execute the java program using the arguments surrounded by “”. It will be like this:

javac Wood.java
java Wood Hello Squirrel from the” “Wood”
Starting the Wood program!!!
Hello Squirrel from the
Wood

If we don’t surround the words with the double quote “” we will have a result something like this:

javac Wood.java
java Wood Hello Squirrel from the Wood
Starting the Wood program!!!
Hello
Squirrel

What is happening here, is that the JVM try to send all the parameters to the args array. However all texts are separated by a white space. The JVM will send all the words as a parameter into the args arrays. Instead of have an array of length of 2, we have an array of length of 5. Lastly, in our code we only handle the first two arguments (the first two positions). And the three last arguments are ignored.

In this lecture we review the main() method, how use it and how to run a Java program. Moreover we explain how to send data to the program through the command line (CLI).

I will see you in the next lecture.

Happy Learning!!!

Leave a comment