Task 1: Explore the Employer Class

Open the Employer file in IntelliJ and examine the code. In addition to the two fields—id and value—the class includes the standard getters and setters as well as some custom methods like toString and equals.

You can refer to these examples as you fill in the missing pieces in the other classes, but for now let’s take a closer look at the constructors.

Assign a Unique ID

One neat trick we can use is to automatically assign each new object a unique ID number.

Example

Examine the two constructors in Employer.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public class Employer {
   private int id;
   private static int nextId = 1;
   private String value;

   public Employer() {
      id = nextId;
      nextId++;
   }

   public Employer(String aValue) {
      this();
      this.value = aValue;
   }

   // Getters and setters omitted from this view.
}
  1. Line 3 declares the variable nextId. Since it is static, its changing value is NOT stored within any Employer object.

  2. The first constructor (lines 6 - 9) accepts no arguments and assigns the value of nextId to the id field. It then increments nextId. Thus, every new Employer object will get a different ID number.

  3. The second constructor (lines 11 - 14) assigns aValue to the value field. However, it ALSO initializes id for the object by calling the first constructor with the this(); statement. Including this(); in any Employer constructor makes initializing id a default behavior.

Next