Results 1 to 9 of 9
  1. #1
    luke14 is offline Member
    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0

    Post comparing two instances of a java class using boolean equals

    Hi

    I have a class called Player and i want to compare two instances of a class so that if their names are equal they are the same. Why does this work?

    public boolean equals(Player other) {

    if (other.getName().equals(this.getName())) {
    if (other.getAge() != this.getAge()) {
    return false;
    } else {
    return true;
    }
    }
    return true;
    }


    Thanks

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: comparing two instances of a java class using boolean equals

    I am not certain what you mean by "Why does this work?" It uses the equals method in String to compare the names. Then it uses the relational operators to compare the ages.

    Note, if you are going to write you own equals method for your class then get in the habit of overriding equals which you inherit from Object.
    The parameter type of equals is Object. And you also need to override hashCode.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    luke14 is offline Member
    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0

    Default Re: comparing two instances of a java class using boolean equals

    Quote Originally Posted by jim829 View Post
    I am not certain what you mean by "Why does this work?" It uses the equals method in String to compare the names. Then it uses the relational operators to compare the ages.

    Note, if you are going to write you own equals method for your class then get in the habit of overriding equals which you inherit from Object.
    The parameter type of equals is Object. And you also need to override hashCode.

    Regards,
    Jim
    Hi

    What i meant was why doesnt this work sorry.

    The problem is I want it to be true when the name are equal disregarding the age, but if the age are the same but the names are different I dont want them to be equal either. It doesnt seem to work for it.

    I am not sure if I am comparing to instances of the object in the right way, basically two instacnes of the abjects with random different names are created and then compared and im not sure weather this compares to right two instances

    Thanks

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: comparing two instances of a java class using boolean equals

    Quote Originally Posted by luke14 View Post
    The problem is I want it to be true when the name are equal disregarding the age, but if the age are the same but the names are different I dont want them to be equal either. It doesnt seem to work for it.
    Based on what you just said above, just compare the names.

    so just do the following:

    return other.getName().equals(this.getName());

    If the names are the same, then you don't care about the ages. If the names are different, you still don't care about the ages.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    luke14 is offline Member
    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0

    Default Re: comparing two instances of a java class using boolean equals

    so you mean like this

    public boolean equals(Player other) {

    return other.getName().equals(this.getName());

    }

    this still doesnt seem to work

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: comparing two instances of a java class using boolean equals

    I assume getName is returning a String. Or is it returning something else? Can you show the Player class? Oh, and I made the assumption that
    the equals method was actually within the Player class. If that is not the case then this may not work.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    luke14 is offline Member
    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0

    Default Re: comparing two instances of a java class using boolean equals

    Yh it is in the class, heres the code;

    class Player {

    static int age;
    static String name;
    static ArrayList<Player> players = new ArrayList<>();;
    static int numOfPlayers;

    public Player(String inputName) {

    name = inputName;
    age = 15;
    numOfPlayers++;

    players.add(this);

    }

    public void printPlayer() {
    System.out.println("Player " + Player.name + " is " + Player.age + " years old");
    }

    public void setAge(int inputAge){
    age = inputAge;
    }

    public int getAge(){
    return age;
    }

    public String getName(){
    return name;
    }

    public int getPlayersNumOf(){
    return numOfPlayers;
    }

    public boolean equals(Player other) {

    return other.getName().equals(this.getName());

    }




    }

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: comparing two instances of a java class using boolean equals

    The problem is that you are declaring all of your values static. That means they are class variables as opposed to instance variables. So all instances are referencing the same value. Remove the static declaration. And as long as you are using getters and setters, make them private.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    luke14 is offline Member
    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0

    Default Re: comparing two instances of a java class using boolean equals

    Thanks that worked

    if i wanted to count the number of instances where the names where the same from the equals method am i on the right track?


    public static int equalPlayersCount(){
    int countSameInstance = 0;

    for(int i = 0; i < players.size(); i++){
    if(name.equals(nonStaticName)){
    countSameInstance++;
    }

    }

    return countSameInstance;
    }
    Last edited by luke14; 10-19-2013 at 02:51 AM.

Similar Threads

  1. How to get multiple instances of an object/class
    By xEthan in forum New To Java
    Replies: 3
    Last Post: 08-04-2013, 01:40 AM
  2. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  3. Java imagemap class gives incosistent instances
    By devdon in forum AWT / Swing
    Replies: 20
    Last Post: 03-17-2012, 02:00 PM
  4. Problem with class instances
    By sdwinder in forum New To Java
    Replies: 7
    Last Post: 10-21-2009, 01:25 AM
  5. Replies: 0
    Last Post: 01-25-2008, 11:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •