Java Main Method

A Java program needs to start its execution somewhere. A Java program starts by executing the main method of some class. You can choose the name of the class to execute, but not the name of the method. The method must always be called main. Here is how the main method declaration looks when located inside the Java class declaration from earlier:

package myjavacode;

public class MyClass {

    public static void main(String[] args) {


The three keywords publicstatic and void have a special meaning. Don’t worry about them right now. Just remember that a main() method declaration needs these three keywords.

After the three keywords you have the method name. To recap, a method is a set of instructions that can be executed as if they were a single operation. By “calling” (executing) a method you execute all the instructions inside that method.

After the method, the name comes first a left parenthesis, and then a list of parameters. Parameters are variables (data/values) we can pass to the method which may be used by the instructions in the method to customize its behavior. A main method must always take an array of String objects. You declare an array of String objects like this:

String[] stringArray

In the main() method example earlier I called the String array parameter args, and in the second example I called it stringArray. You can choose the name freely.

After the method’s parameter list comes first a left curly bracket ({), then some empty space, and then a right curly bracket (}). Inside the curly brackets you locate the Java instructions that are to be executed when the main method is executed. This is also referred to as the method body. In the example above there are no instructions to be executed. The method is empty.

Let us insert a single instruction into the main method body. Here is an example of how that could look:

package myjavacode;

public class MyClass {

  public static void main(String[] args) {
    System.out.println("Hello World, Java app");

Now the main method contains this single Java instruction:

System.out.println("Hello World, Java Program");

This instruction will print out the text Hello World, Java Program to the console. If you run your Java program from the command line, then you will see the output in the command line console (the textual interface to your computer). If you run your Java program from inside an IDE, the IDE normally catches all output to the console and makes it visible to you somewhere inside the IDE.

0 0 vote
Article Rating

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x