Quick Index
Overview


The method "main" is a special method.

A class does not generally need to have this method (many classes will not have it).

However, for classes that you will "run", then 'main' is required.

Java will start "running" the program by calling the "main" method. The method main is called the program "entry point".

Method Header for main


The method header (a long one) for "main" will always look exactly like this:

public static void main(String[] args)


Thus we can copy-paste it if desired when we need it to avoid the typing.

Keep It Short


Generally, keep the code in the 'main' method short, and call regular instance methods to do the real work.

Here is a pretty common template of what a 'main' method might look like.

You would change "RectangleTests" to your actual class name.
	public static void main(String[] args)  {
		RectangleTests test = new RectangleTests();
		test.testSetSmall();
		test.testSetLarge();
	}
Here is what "main" might look like for a RectangleTests class.

Also see next example.
public class RectangleTests {

	public void testSetSmall() {

	}

	public void testSetLarge() {

	}

	//=======================================

	public static void main(String[] args) {
		RectangleTests test = new RectangleTests();
		test.testSetSmall();
		test.testSetLarge();
	}

}
Here is what main might look like for a PersonTest class.
public class PersonTests {

	//=======================================
	//Instance Methods

	public void testBasic() {
		//Code to test objects here...
	}

	//=======================================
	//Entry point when we run this class

	public static void main(String[] args) {
		PersonTests personTest = new PersonTests();
		personTest.testBasic();
	}

}