class Users{
private LinkedHashSet<Users> userList=new LinkedHashSet<Users>();
private String name;
public Users(String name){
this.name=name;
userList.add(this);
}
public void iterateList(){
for (Users user:userList){
System.out.println(user.getName()); //NO OUTPUT WHEN CALLED
}
}
public int getTotalUserCount(){
return userList.size(); // OUTPUT IS 0
}
}
I'm trying to add the instances of Users
to userList
when they are created. For some reason, when the userList
is iterated through, I find that the objects weren't added.
Also,
However, an accessor method getName()
works when called on the object individually.
What am I doing wrong?