So i can get access to the list with a getter but how do i add a new user to the list ? What i need to add ?
private List<User> userList= new ArrayList<User>(){{
add(new User("ABC","123","B","b"));
add(new User("CBA","123","I","b"));
}};
public List<User> getUserList() {
return userList;
}
Yes i have added 2 users but they are added when i create the list, I wan’ted to have some kind of a method that i can call and it adds a new user to the list. Something like
public void addNewUser(User){
//some kind of code
}
You have added two users here already by using a double brace initialisation block. To add outside of the initialisation block simply run
Or if you have not got access to userlist then
This second way with the add method follows this pattern (tell dont ask)