1

I have an array in the main class which holds Employee class objects. I'm trying to generate a unique ID for each object but it is printing the same ID for all the objects

Main class:

public static void main(String[] args) {
    Employee employee1 = new Employee("Luke", 36);
    Employee employee2 = new Employee("Martin", 49);
    Employee employee3 = new Employee("Kevin", 21);
    Employee employee4 = new Employee("Sam", 43);
    Employee employee5 = new Employee("Nicole", 45);
    Employee employee6 = new Employee("Linta", 21);

    Employee[] allEmployees = { employee1, employee2, employee3, employee4, employee5, employee6 };

    for (int i = 0; i < allEmployees.length; i++) {
        System.out.println(allEmployees[i]);
    }

}

Employee class

public class Employee {

    private String name;
    private int age;
    private static String employeeID = "0";

    Employee(String name, int age) {
        this.name = name;
        this.age = age;
        employeeID = getNextUniqueID();
    }

    public String getName() {
        return name;
    }
    
    public String setName(String name) {
        this.name =name;
    }


    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    
    public static String getNextUniqueID() {
        int id = Integer.parseInt(employeeID);
        ++id;
        return Integer.toString(id);

    }
    
    public String getEmployeeID() {
        return employeeID;
    }

    public String toString() {
        return  getName() + " " + getAge() + " " + getEmployeeID();
    }
}

I want the employeeID as string and I can't use java.util.UUID; for my project.

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • See the paragraph "Static Fields" in the accepted answer of the linked duplicate questions. That you're using arrays the the other question a list doesn't matter in this context here. – Tom Feb 27 '21 at 05:35
  • 1
    you initialzied employee id as "0" for every employee and incrementing it by 1 in getNextUniqueID. So it is printing the same id for every employee object – Ran94 Feb 27 '21 at 05:37
  • Static properties are shared between all instances of a class hence when you modify the property it's modified for all instances. You should make `employeeID` non-static if it must be unique for each instance. – Nikolai Shevchenko Feb 27 '21 at 05:44
  • You also need to update `employeeID` after each call to `getNextUniqueID()`. – AKSingh Feb 27 '21 at 05:46
  • Ok, I changed to `private String studentID = "0"; ` Now, `int id = Integer.parseInt(studentID);` telling me to make studentID static because the method is `public static String getNextUniqueID()` is static. As per my project, I have to make getNextUniqueID static – Joseph Martínez Feb 27 '21 at 06:12
  • @Tom If i remove static from the data field, I won't be able to use it on getNextUniqueID() method because it is static. – Joseph Martínez Feb 27 '21 at 06:24

2 Answers2

6

You need a static variable associated with the class to maintain the unique id and an instance variable to keep that particular employee's ID in the class.

private String employeeID; // instance member
private static String uniqueID = "0"; // static class variable
public static String getNextUniqueID() {
    int id = Integer.parseInt(uniqueID);     // get the static variable
    ++id;                                    // increment it
    uniqueID = Integer.toString(id);         // update the static variable
    return uniqueID;                         // return the value to use for the employee 
}

Then in the Employee constructor, use the static member:

Employee(String name, int age) {
    this.name = name;
    this.age = age;
    employeeID = Employee.getNextUniqueID();
}

updated Employee class:

public class Employee {

    private String name;
    private int age;
    private String employeeID;
    private static String uniqueID = "0";

    Employee(String name, int age) {
        this.name = name;
        this.age = age;
        employeeID = Employee.getNextUniqueID();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public static String getNextUniqueID() {
        int id = Integer.parseInt(uniqueID);
        ++id;
        uniqueID = Integer.toString(id);
        return uniqueID;
    }

    public String getEmployeeID() {
        return employeeID;
    }

    public String toString() {
        return getName() + " " + getAge() + " " + getEmployeeID();
    }
}

Output:

Luke 36 1
Martin 49 2
Kevin 21 3
Sam 43 4
Nicole 45 5
Linta 21 6

geocodezip
  • 158,664
  • 13
  • 220
  • 245
1

You should store last generated id in static field but use non static for id of certain employee.

Also you should use AtomicInteger type for thread safety which you can convert to String. Check that:

import java.util.concurrent.atomic.AtomicInteger;

public class Employee {

    private String  employeeID;
    private String name;
    private int age;
    private static AtomicInteger lastGeneratedId = new AtomicInteger(0);

    Employee(String name, int age) {
        this.name = name;
        this.age = age;
        employeeID = getNextUniqueID();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public static String getNextUniqueID() {
        return String.valueOf(lastGeneratedId.incrementAndGet());
    }

    public String getEmployeeID() {
        return employeeID;
    }

    public String toString() {
        return getName() + " " + getAge() + " " + getEmployeeID();
    }

    public static void main(String[] args) {
        Employee employee1 = new Employee("Luke", 36);
        Employee employee2 = new Employee("Martin", 49);
        Employee employee3 = new Employee("Kevin", 21);
        Employee employee4 = new Employee("Sam", 43);
        Employee employee5 = new Employee("Nicole", 45);
        Employee employee6 = new Employee("Linta", 21);

        Employee[] allEmployees = { employee1, employee2, employee3, employee4, employee5, employee6 };

        for (int i = 0; i < allEmployees.length; i++) {
            System.out.println(allEmployees[i]);
        }
    }
}

geocodezip
  • 158,664
  • 13
  • 220
  • 245
Sergey Afinogenov
  • 2,137
  • 4
  • 13