1

This is an update of the same question, which is partly solved.

Java - Object properties does not differ

int size = 0;
int new = 10;
int default = 100;

ClassList filterList = new ClassList();
filterList.setSize(size);
filterList.setNew(new);
filterList.setDefault(default);

// Now i am creating another instance of the ClassList.

ClassList newList = ClassList(); 

Its strange that my newList has values of size, new and default, but with values as 0, 0 and 0 I tried to check whether the instances are same or not.

if(filterList .equals(newList)){
    Log.d("Is it equal","True");
    } else {
    Log.d("Its Not equal","false");
}

I got it false.

My POJO class is plain simple with getters and setters.

public class ClassList{

    private int size;
    private int new;
    private int default;

    public int getPageSize() {
        return size;
    }
    public void setPageSize(int size) {
        this.size = size;
    }
}
2
  • 2
    since when can new and default be used as field names?
    – jlordo
    Nov 2, 2012 at 11:32
  • your code wouldnt even compile... you are using new and default as variable names Nov 2, 2012 at 11:36

4 Answers 4

2

Default value for int is 0 that is why size, new and default has values 0, 0 and 0 on newly created instance

Default equals comparison happens on == that is reference equality. You will need to override equals and hashcode

From Java doc of Object#equals

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

Sample Implementaion :

public static class ClassList {

    private int size;
    private int newValue;
    private int defaultValue;

    public int getPageSize() {
        return size;
    }
    public void setPageSize(int size) {
        this.size = size;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null)
            return false;
        if (!(obj instanceof ClassList))
            return false;
        ClassList c = (ClassList) obj;

        return this.size == c.size && this.newValue == c.newValue
                && this.defaultValue == c.defaultValue;
    }

    @Override
    public int hashCode() {
        return Integer.valueOf(size).hashCode()
                + Integer.valueOf(newValue).hashCode()
                + Integer.valueOf(defaultValue).hashCode();
    }
}

Note: new and default are reserved keywords in java. You can not define variable names for reserve keywords.


References:

2
  • Can you show me out on how would i call the equals method in my other class and print out the results in log. thanks
    – theJava
    Nov 2, 2012 at 12:18
  • Default value for int is 0 that is why you have values of size, new and default. The way you are calling equals is fine. Nov 2, 2012 at 12:24
1

Override the equals and hashCode methods in your ClassList class.

You can do this with Eclipse by right clicking in the java file -> Source -> Generate hashCode() and equals().

1

Object equality in Java is not determined by the state fo the object's fields per default - each instance is different from any other.

You will have to override the equals method to make it return true depending on the object state.

Implementing equals correctly might not be as straightfoward. Look up the API docs for the equals method for implementation advice.

1

You need to override equals and hashcode method for custom equaling.

public class ClassList{

    private int size;
    private int new;
    private int default;

    public int getPageSize() {
        return size;
    }
    public void setPageSize(int size) {
        this.size = size;
    }
    public boolean equals(Object obj){
         if (obj == null)
             return false;
         else if (obj == this)
             return true;
         else if (obj.getClass() != getClass())
             return false;
         else
             return this.hashCode()==obj.hashCode();
    }
    public int hashCode(){
       return size+new+default;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.