Adding objects to an ArrayList
My intention was to create an ArrayList of "Employee" objects. Each "Employee" has a name and a id. My Employee class looks like this:
package test;
publicclass Employee{
private String name =null;
privateint id = 0;
public Employee(){
}
publicvoid setName(String n){
name = n;
}
public String getName(){
return name;
}
publicvoid setID(int i){
id = i;
}
publicint getID(){
return id;
}
}
Then I have a main class which is suppose to set the names and id's then add the object to the array list
package test;
import java.util.ArrayList;
publicclass Main{
private Employee e =new Employee();
private ArrayList<Employee> employees =new ArrayList<Employee>();
public Main(){
generateEmployees();
for(int i = 0; i < employees.size(); i++){
System.out.println(employees.get(i).getName());
}
}
publicvoid generateEmployees(){
//Add Joe
e.setName("Joe");
e.setID(1);
employees.add(e);
//Add Bob
e.setName("Bob");
e.setID(2);
employees.add(e);
}
publicstaticvoid main(String[] args){
new Main();
}
}
But when I run it, rather than seeing Joe and Bob is see Bob and Bob, what am I doing wrong?

