Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Two different Java objects are always equal regardless of fields value

New member
Joined
Feb 27, 2023
Messages
2
I have classes defined like below:

Code:
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder

public abstract class A {
    private Enum myEnum;
}

Code:
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder

public class KA extends A {
    private MyPackage pck;
    private String field1;
    private String field2;
}
I tested them with the following code:

Code:
KA ka1 = new KA();
KA ka2 = new KA();
System.out.println("Object equal: " + ka1.equals(ka2));
For KA, either no-arg constructor or constructor with totally different arguments, I would get they are equal.
I want KA to be using the default equal method of Object class which is comparing the object memory addresses.
What is wrong here?
 
Top