0

I have this Person class with some methods to add, remove and view your friends. For some reason, when I want to view my friend, even though I think I'm calling the right methods the right way, The last Person object (which is carl ) is printed regardless of the object specifications in the method call. What is the problem?

here is the code:

package person;

public class Person {
    private static String name;
    private static String friends;

    public static void main(String[] args) {

        Person ted = new Person ("ted");
        Person jim = new Person ("jim");
        Person todd = new Person ("todd");
        Person tom = new Person ("tom");
        Person carl = new Person ("carl");

        // apparently I'm making a mistake here... 
        jim.addFriend(zack);
        System.out.println(jim.getFriends());
    }
    public Person (String aName) {
        name = aName;
        friends = "";
    }
    public static void addFriend(Person friend) {

        friends = friends + friend.name + " ";
    }
    public static void unFriend (Person nonFriend) {
        friends = friends.replace(nonFriend.name + " ", "");
    }
    public static String getFriends () {
        return friends;
    }

}
zamzam
  • 347
  • 1
  • 2
  • 9
  • Because they are `static` try removing that. `Static` pretty much means it is the same for all instances created of that class. – 3kings Apr 01 '16 at 14:41
  • Remove all the static except for main. A static field will exist only once, is a field of the class object, and one could do `Person.name = "any";` – Joop Eggen Apr 01 '16 at 14:42
  • Related: http://stackoverflow.com/q/7026507/1079354 – Makoto Apr 01 '16 at 14:50

2 Answers2

3

You are using static variable. Please try this.

package person;

public class Person {
    private  String name;
    private  String friends;

    public static void main(String[] args) {

        Person ted = new Person ("ted");
        Person jim = new Person ("jim");
        Person todd = new Person ("todd");
        Person tom = new Person ("tom");
        Person carl = new Person ("carl");

        // apparently I'm making a mistake here... 
        jim.addFriend(zack);
        System.out.println(jim.getFriends());
    }
    public Person (String aName) {
        name = aName;
        friends = "";
    }
    public void addFriend(Person friend) {

        friends = friends + friend.name + " ";
    }
    public void unFriend (Person nonFriend) {
        friends = friends.replace(nonFriend.name + " ", "");
    }
    public String getFriends () {
        return friends;
    }

}
RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
Abhijit
  • 746
  • 5
  • 18
0

Make

private static String name;
private static String friends;

To

private String name;
private String friends;

and change methods by removing static too

   public void addFriend(Person friend) {

        friends = friends + friend.name + " ";
    }
    public void unFriend (Person nonFriend) {
        friends = friends.replace(nonFriend.name + " ", "");
    }
    public String getFriends () {
        return friends;
    }
Rajarshi Das
  • 11,778
  • 6
  • 46
  • 74