Details of a program which does nothing!

In this post I will be discussing about the main function. The program that we will would be discussing is as follows:

class DoNothing{
    public static void main( String []args ){


The name of the class (DoNothing) reflects the purpose of the program which is essentially to do nothing. The above is a minimal code which would compile and run in Java. The program would start and finish immediately.

Now let us discuss the program in detail. Line number 1 and line number 5, that is class DoNothing{ … } declares a class DoNothing which in this case is encapsulating a single function main().  The keyword class begin the class definition for the class DoNothing. The code for class DoNothing appears between the opening and closing curly braces.

A Java program can contain multiple class definitions, which may or may not be in a single .java file. Compilation of a .java file leads to the creation of .class file which is executed by java. When a .java file is compiled using the java compiler, each class present in the .java file leads to a creation of separate .class file.

In the Java programming language, every application must contain a main method whose signature is:

public static void main(String[] args)

The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above. You can name the argument anything you want, but usually its named as args or argv. The main() function accepts a single argument of type string. This string array stores the command lines arguments which may be passed while initiating the  application. For example, if we execute the above application using the following command line:

java DoNothing one "two three" four

The array args would contain three element, viz- args[0]=”one”, args[1]=”two three” and args[2]=”four”. The same can be done using an IDE like BlueJ by typing {“one”,”two three”,”four”} in the dialogue box which open on choosing the “void main(String[] args)” from the context menu. Also note that both String args[] and String []args are valid.


Passing command line arguments via BlueJ


The function main() is declared public because it is to be invoked by Java loader which is outside the class.

The function main() is declared static so that it can be invoked without first creating an object of the containing class. Please note it’s not possible to create an object at the time of invoking main() because an object can only be created once the program starts. The loader can invoke the main()  function as DoNothing.main() if it is static.

The main( ) function is the entry point of the program and other methods, if any, are invoked from here. A program terminates all its activity and exits when one of two things happens:

  1. All the threads that are not daemon threads terminate.
  2. Some thread invokes the exit method of class Runtime or class System and the exit operation is not forbidden by the security manager.

The function main() is declared void because the program may exit before or after the main method finishes as discussed above and a return value from main would therefore be meaningless. However, it’s possible to return an exit value by System.exit(int status), Runtime.exit(int status) or Runtime.halt(int status) but these are outside the scope of ICSE/ISC computer science/application scope. Interested students may refer to the documentation of the same for details.



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.