main

When we run a class directly, then that class needs a "main" method. This tells Java where to start running the program. It is Java's execution "entry point".

Below is an example of a class with a main method.

The "main" method header is always as shown below.

In "main" below, we construct a "Time" object, and then send a message to it, asking it to show the time.

We have encountered the main method in our "Tester" classes.

import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;

public class Time {

	public static void main(String[] args) {
		Time timeObject = new Time();
		timeObject.showTime();
	}

	public void showTime() {
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss a");
		System.out.println("Current Date and Time: " + dateFormat.format(new Date()));
	}

}