I've run into an interesting problem wherein I am creating an array of objects with their values initialized in the constructors parameters, but the output (below in for loop) shows that the final element initialized will overwrite all other elements. I've hit a mental brick wall if anyone can help it would be greatly appreciated!
public class SoftwareServicesLtd
{
public static void main(String[] args)
{
Employee.employees[0] = new Employee("John", "Smalls", "Manager", "None", "None", 24000, "Human Resources");
Employee.employees[1] = new Employee("Devin", "Nathan", "Manager", "None", "None", 24000, "Software Development");
Employee.employees[2] = new Employee("Rhianna", "Mardsen", "HR Staff", "Devin", "None", 18000, "Human Resources");
Employee.employees[3] = new Employee("Ryan", "Tepper", "Developer", "John", "C#", 20000, "Software Development");
Employee.employees[4] = new Employee("Harry", "Wainwright", "Developer", "None", "Java", 15000, "Software Development");
for(int i= 0; i < Employee.employees.length; i++)
{
System.out.println(i + " - " + Employee.employees[i].nameFirst_);
}
Employee class and constructor:
public class Employee extends SoftwareServicesLtd {
static String nameFirst_, nameLast_, employeeType_, skillSet_, department_ = " ";
static String[] managers_ = new String[5];
static double salary_ = 0;
static Employee[] employees = new Employee[5];
public Employee(String first, String last, String employeeType, String manager, String skillSet, double salary, String department)
{
nameFirst_ = first;
nameLast_ = last;
employeeType_ = employeeType;
managers_[0] = manager;
skillSet_ = skillSet;
salary_ = salary;
department_ = department;
}
Expected Output:
0 - John
1 - Devin
2 - Rhianna
3 - Ryan
4 - Harry
Actual Output:
0 - Harry
1 - Harry
2 - Harry
3 - Harry
4 - Harry