I am creating an arraylist of objects and trying to print them out. However, when I go to print it is not finding the method.
Researched arraylist Objects and copied some other code. I just can not seem to find the problem with my code.
public class Main{
public static void main(String[] args){
List<PhoneBook> users = new ArrayList<>();
users.add(new PhoneBook("Paul", 4129991));
users.add(new PhoneBook("Kelly", 5702135));
for(int i = 0; i<users.size(); i++){
System.out.println("PhoneBook " + PhoneBook.getName());
}
}
}
class PhoneBook{
private String name;
private int phoneNumber;
public PhoneBook(String name, int phoneNumber){
this.name = name;
this.phoneNumber = phoneNumber;
}
public String getName(){
return name;
}
public int getPhoneNumber(){
return phoneNumber;
}
public void setName(String name){
this.name = name;
}
public void setPhoneNumber(int phoneNumber){
this.phoneNumber = phoneNumber;
}
}
PhoneBook.getName()tousers.get(i).getName(). There is nostaticmethodgetName()onPhoneBook, and you can also look at it this way: you're looping over a list of multiple users, but you're not in any way telling the compiler which user from the list you want to get the name for. That can't be right.