Before you begin

This unit is part of the “Intro to Java programming” learning path. Although the concepts discussed in the individual units are standalone in nature, the hands-on component builds as you progress through the units, and I recommend that you review the prerequisites, setup, and unit details before proceeding.

Unit objectives

  • Learn how an object, or collection of objects, becomes an application
  • Use Eclipse to create a driver class

The application entry point

All Java applications need an entry point where the Java runtime knows to start executing code. That entry point is the main() method. Domain objects — that is, objects (Person and Employee, for example) that are part of your application’s business domain— typically don’t have main() methods, but at least one class in every application must.

As you know, Person and its Employee subclass are conceptually part of a human-resources application. Now you’ll add a new class to the application to give it an entry point.

Creating a driver class

The purpose of a driver class (as its name implies) is to “drive” an application. Notice that this simple driver for a human-resources application contains a main() method:


package com.makotojava.intro;
public class HumanResourcesApplication {
  public static void main(String[] args) {
  }
}

Now, create a driver class in Eclipse using the same procedure you used to create Person and Employee. Name the class HumanResourcesApplication, being sure to select the option to add a main() method to the class. Eclipse will generate the class for you.

Next, add some code to your new main() method so that it looks like this:


package com.makotojava.intro;
import java.util.logging.Logger;

public class HumanResourcesApplication {
  private static final Logger log = Logger.getLogger(HumanResourcesApplication.class.getName());
  public static void main(String[] args) {
    Employee e = new Employee();
    e.setName("J Smith");
    e.setEmployeeNumber("0001");
    e.setTaxpayerIdentificationNumber("123‑45‑6789");
    e.setSalary(BigDecimal.valueOf(45000.0));
    e.printAudit(log);
  }
}

Finally, launch the HumanResourcesApplication class and watch it run. You should see this output:


Sep 19, 2015 7:59:37 PM com.makotojava.intro.Person printAudit
INFO: Name=J Smith,Age=0,Height=0,Weight=0,EyeColor=null,Gender=null TaxpayerIdentificationNumber=123‑45‑6789,EmployeeNumber=0001,Salary=45000.00

That’s all there is to creating a simple Java application. In the next unit, you begin looking at some of the syntax and libraries that can help you develop more-complex applications.

Previous: ExceptionsNext: Inheritance