Q:

How to add an element in an instanced class arraylist

I have this code to create a class User
public class User {

    private String name;
    private ArrayList<User> owners = new ArrayList<>();

    public User(String name, ArrayList<User> owners) {
        this.name = name;
        this.owners = owners;

    }

    public String getName() {
        return name;
    }

    public void addOwner(User owner) {
        owners.add(owner);

    }

If i create an instance of this class with
User jhil = new User(name, new ArrayList<>());

What do i do to add a string element to the arraylist
I've tried with the addOwner method with
jhil.addOwner(jhilsara);

but i get a the method addOwner(String) is undefined for the type User error
I've also tried with the ArrayList add method
jhil.add(jhilsara);

But that doesn't work either.
So my question is what do i need to do in order to add something to the arraylist of an instanced of my class User

A:

You have your ArrayList set to contain objects of the User class, not Strings. Change the declaration of it to:
private ArrayList<String> owners = new ArrayList<>();

Then, you also have to change addOwner to:
public void addOwner(String owner) {
    owners.add(owner);
}

