Object reference case study

Posted by Skogen on Stack Overflow See other posts from Stack Overflow or by Skogen
Published on 2010-05-19T16:06:54Z Indexed on 2010/05/19 16:10 UTC
Read the original article Hit count: 194

Filed under:
|

When person 1 become partner with person 3, person 2 should no longer have person 1 as partner. How should I solve this?

public class Person {

    private String name;
    private Person partner;

    public Person(String name){
        this.name = name;
    }

    public void setPartner(Person partner){
        this.partner = partner;
        partner.partner = this; 
    }

    public static void main(String[] args) {

        Person one = new Person("1");           
        Person two = new Person("2");
        Person three = new Person("3");
        Person four = new Person("4");

        one.setPartner(two);
        three.setPartner(four);

        one.setPartner(three);

        //Person two is still partner with person 1 
   }

© Stack Overflow or respective owner

Related posts about java

Related posts about homework