Using the static keyword in Java to increment an object index number
In this article I will show how to use Java's static keyword to increment a unique index number for object instances.
We will start by creating a simple class called Employee comprising fields only for name and address. These fields will be normal instance variables:
public class Employee {
private String name;
private String address;
public Employee(String name, String address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Employee{" + "name=" + name + ", address=" + address + '}';
}
}
The Employee class defines two instance variables for the fields name and address, along with a constructor requiring these values and getters and setters. A toString() method is provided since this is good practice for most Java classes.
Being instance variables, the fields name and address can have individual values for each object instance of Employee that is created, as demonstrated below:
Employee emp1 = new Employee("Fred", "1 Rock Avenue");
Employee emp1 = new Employee("Barney", "2 Boulder Street");
If you were to call the getName() method on object emp1 it would return "Fred", while calling getName() on emp2 would return "Barney".
The static keyword
The purpose of the Java static keyword is define a variable whose value is shared among all instance variables. That is, each object instance will have the same value at any point in time. If the static variable's value is changed by any one object instance then that change is automatically reflected in all the instances.
Let's define an integer static variable in Employee called nextIndex and set it to have a value of 1.
public class Employee {
private static int nextIndex = 1;
private String name;
private String address;
// remainder of code omitted
The purpose of nextIndex is to help us generate the next available number for the object instance, so we need to declare another instance variable to store this. We will call this index:
public class Employee {
private static int nextIndex = 1;
private int index;
private String name;
private String address;
// remainder of code omitted
In the constructor we can set index to the current value of the static nextIndex, followed by incrementing the value of nextIndex ready for the following time the constructor may be called:
public Employee(String name, String address) {
this.index = Employee.nextIndex;
Employee.nextIndex++;
this.name = name;
this.address = address;
}
You can see this above with Employee.nextIndex
Finally, we will change the toString() method to include the index field so we can more easily verify that it is working:
@Override
public String toString() {
return "Employee{" + "index=" + index + ", name=" + name + ", address=" + address + '}';
}
Testing the code
Testing that our code works is straightforward. We can just instantiate a number of Employee objects and output their toString() results. Each new instance will have its index value one more than the previous:
Employee emp1 = new Employee("Fred", "1 Rock Avenue");
System.out.println(emp1);
Employee emp2 = new Employee("Barney", "2 Boulder Street");
System.out.println(emp2);
The above should result in the following output:
Employee{index=1, name=Fred, address=1 Rock Avenue}
Employee{index=2, name=Barney, address=2 Boulder Street}
Each time a new Employee instance is created, its index value will be incremented for you.