Add an object to another object contained in one of the first's class attributes, Java

Luyw :

I have a class called Club that contains an ArrayList of persons, which are objects. Each Person has a club attribute of type Club. To add persons to the club, I have made a method, addMember, that takes a Person object and adds it to the desired ArrayList.

Is it possible to do something like this?

public class Club {
...
    public void addMember(Person person) {
        person.setClub(**This instance of club**);
        this.memberList.add(person);
    }
...
}
Johannes Kuhn :

Use this to reference this instance. It's not only a way to access properties and methods, but also a reference.

person.setClub(this);

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=407287&siteId=1