Home > other >  What replace this in a Static Method?
What replace this in a Static Method?

Time:03-19

I'm studying Java, i have to create a Static equal method in a generic class....in a non static class i use this. to refer to the object who call the equals, but in a static method how can i do?

> public static <T> boolean equals2(T object) {
        if (GenericClass == object) return true;
        if (object == null || GenericClass.class != object.getClass()) return false;
        GenericClass<?> that = (GenericClass<?>) object;
        return Objects.equals(object, that.t);
            }

I'm trying this but it gave me error on generic Class

CodePudding user response:

public static boolean equals2(T object) {

That method signature doesn't make sense because you only have 1 object so there isn't anything to compare it to. If that needs to be a static method then it would make more sense to have the equals2 method accept the 2 objects that you want to compare. There are many utility methods that do this sort of thing. One is https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Objects.html#equals(java.lang.Object,java.lang.Object).

CodePudding user response:

The signature of the function should be different, you should get two objects The first of type T And the other of the object type.

  •  Tags:  
  • list
  • Related