public static void main string arguments

public static void main string arguments introduction

public static void main string arguments method is the entry point to any java program.

Here, we speak about desktop applications. In the web context, there is a container that will manage the startup and the shutdown of a java application.

We will present a simple example that shows how to use the public static void main string args in java :

public class TestMain {

               public static void main(String[] args) {

                              System.out.println("Hello JAVA from how-to-program-in-java.com");

               }

}

The public static void main string args method must go inside a class. There are no standalone methods in java. Everything goes into java classes.

The class that contains the public static void main method is the principal class.

Here, our main class is called TestMain.

The main method is the method that gets executed when our program executes.

So, when we create classes and we don’t use them inside the main method, those classes aren’t actually used by our program: They are useless.

In fact, we use classes inside the main method by creating java objects based on those classes.

The program above prints in the console the message: Hello JAVA from how-to-program-in-java.com

public static void main string arguments explained

Now, we will explain the keywords present in the signature of the main method.

public : defines the visibility of the public static void main(String args) method. It means that this method can be accessed by any other class with no problem.

In fact, this method will not be accessed by others classes. It will be executed by the JVM when our program runs. But, keeping the public keyword is mandatory to let the JVM execute this method.

static:  This keyword means that the main method belongs to the class itself and doesn’t belong to instances of this class. This keyword allows us to call a method without creating an instance of the class that contains this method.

In our case, the JVM will call this method without creating an instance of the class TestMain.

This will save a little bit of memory.

void:  this keyword means that the public static void main method doesn’t return anything.

So, we don’t expect any returned value from the main method.

String [] args: this is a java array of strings. It can be used to pass arguments to the main method when executing it. The type of elements in the array is String because we can convert any type to a string.

Also, we can change the name of the array (args) with no problem. It is dealt with like any name of a formal argument of a regular method.

Running public static void main string args from the command line

public static void main string args running without command line parameters

Here we will run the program above from within the command line.

In order to follow the examples, you need to set the JAVA_HOME, classpath and add java to the path variable.

In order to set your environment, please feel free to check our tutorial: install the necessary tools

We should follow those steps:

1)     We begin by saving the program above in a file called: TestMain.java.

Note that the name of the file must match the name of the public class contained in that file. Also, a source file in java contains no more than one public class.

2)     we put this file in this path: C:\program1\TestMain.java

public static void main string arguments step1

3) We open the command line (cmd) and we type those commands

4) cd C:\program1

public static void main string arguments step2 

5)     javac TestMain.java

public static void main string args step3

6)     java -classpath . TestMain

      The output of the program is the following:

public static void main string args step5

In step 5, we compiled the class TestMain. After launching this command, we notice that we have an extra file in the C:\program1 directory.

public static void main string arguments step5

This file contains the byte code generated after compiling the class TestMain.

In step 6, the java command invokes the JVM. The latter will run the compiled version of the class TestMain.

public static void main string arguments running with command line parameters

This is our java program:

public class TestMainWithArguments {

               public static void main(String[] args) {

                              for(int i = 0; i< args.length; i++){

                                            System.out.println("Hello "+ args[i]);

                              }            

               }

}

 In this section, we just repeat the same steps until step 6.

In fact, we just replace TestMain with TestMainWithArguments.

In step 6, we will run this command:  java -classpath . TestMainWithArguments john peter

Here, we passed two parameters to our public static void main string args method: john and peter.

The arguments are separated by a space.

The output of this program is the following:


This tutorial that explains what is   public static void main string arguments arrives at its end.

Please don’t forget to subscribe to like our Program In JAVA Facebook page or to subscribe to our newsletter to get our latest JAVA tutorials.

To check our core java tutorial, please check this link.

See you in the next core java tutorial.

Post a Comment

Previous Post Next Post