Line by line analysis of HelloWorld

If you recall the code for the HelloWorld program was

public class HelloWorld{
public static void main(String argv[]){
        System.out.println("Hello World");

To take the first line of the program

public class HelloWorld{

The keyword public indicates that this class will be visible from anywhere. This is known as an access modifier. Later you will learn about the modifiers private and protected. The keyword class indicates that this is the start of the most basic building block of Java. Classes are sometimes defined as

an aggregate of data and executable code

The executable code that in other languages might be called functions or subroutines are called methods in Java. The next line is an example of a method.

public static void main(String argv[]){

There is a certain "magic" about the main method in Java in that it is the place where execution of any application starts. The main method must always have the same format or signature as in this example.

The keyword public has a similar meaning for a method as for a class. It indicates that the method can be called from anywhere. The keyword static indicates that this method can be called without creating an instance of the class. If that means nothing to you, don't worry about it as it will be explained later. The keyword void indicates that this method will return no value. The name of the method is main. A method can be given any any name so long as it begins with a non-numeric character and does not contain spaces. You can start a method name with a dollar sign, but I wouldn't recommend it.

The parameters for the method are contained within the parenthesis, in this case

(String argv[])

This indicates that when the method is called it must be passed a String array. An array is a collection of elements all of the same data type. In this case we have an array of the type String. The Java Virtual Machine fills this array with any parameters passed to the program on the command line. This is often how programs are started in the world of Unix and older DOS programs but is not so common with GUI based programs. If you were to start the program with the command line

java HelloWorld one two three

The argv array would contain the Strings

"one" "two" "three" 

which the program could then access, you could print out the first argument to the program (the first text after the program name) with a line as follows.


See how the first element is zero and not one.

Note that the data type String begins with an upper case letter S. Note that the parameter name argv[] is just a convention, it would be just as correct to create a main method as

public static void main(String foo[])

However it is a good idea to stick to the convention of calling the parameter argv[] to make your code easily understandable.

System.out.println("Hello World");

This is a call to the static method


Note the use of the dot notation, i.e. the dots between System, out and println. This means that println is a method of the class out that is a child of the class System. The System class is a low level class that any program can have access to at any time. The println method expects to receive a string for a parameter and in this case I have obliged by passing the "Hello World" string I want to send to the console. Note how the line is terminated by a semi colon as you would do in C/C++.

The next two lines simply consist of closing curly braces. The first one indicates the closure of the main method and the next indicates the end of the class. Note how the end of the class does not require a semi-colon.

Last modified: Thursday, 24 July 2014, 2:54 PM