import java.util.ArrayList;
public class ByReference {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Employee> emps = new ArrayList<Employee>();
// create employee objects
Employee emp1 = new Employee("A");
Employee emp2 = new Employee("T");
Employee emp3 = new Employee("K");
// add employee objects to array list
emps.add(emp1);
emps.add(emp2);
emps.add(emp3);
// print the array list
System.out.println(emps);
// remove one of the employees
emps.remove(emp2);
// print the array list again
System.out.println(emps);
}
}
class Employee{
private String name;
public Employee(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Employee [name=" + name + "]";
}
}
Console:
[Employee [name=A], Employee [name=T], Employee [name=K]]
[Employee [name=A], Employee [name=K]]