The main method

Every Java application must have a class that contains a method called main. It is this class that is called when the program is run. If you do this with the java tool and the name of that class is Smart, you would do

java Smart

The main method takes an argument consisting of an array of strings. We'll learn more about arrays and strings later. For now, you can just think of this as a list of symbols. When you call the main method, and thing that appears after the name of the class will get passed to the method in this array of strings. The main method can then use one or more of the strings in some way. For example, the following definition of main passes the whole array of strings to the init method in the Beh class.

public static void main(String args[]) {
  Beh.init(args);
}

To call this method, you put whatever you want to be passed to the init method after the name of the class with the main method. If this is a class called Smart, you could do this, for example:

java Smart world2

This would create an array of strings with just one string in it, "world2", and would pass this array to the init method in the Beh class.

Home

Calendar

Coursework & grading

Assignments

Lecture notes

Other resources


IU home

IU CS home

Contact instructor