Guest User

Untitled

a guest
Jun 22nd, 2022
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.55 KB | None | 0 0
  1. public class Eq {
  2.     static class A {
  3.         static int j = 1;
  4.         int i;
  5.         A(int i) {
  6.             this.i = i;
  7.         }
  8.  
  9.         @Override
  10.         public int hashCode() {
  11.             return ++j;
  12.         }
  13.  
  14.         @Override
  15.         public boolean equals(Object obj) {
  16.             if (this == obj)
  17.                 return true;
  18.             if (obj == null)
  19.                 return false;
  20.             if (getClass() != obj.getClass())
  21.                 return false;
  22.             A other = (A) obj;
  23.             return i == other.i;
  24.         }
  25.     }
  26.    
  27.     @Test
  28.     void  testEquals() {
  29.         A a1 = new A(1);
  30.         A a2 = new A(1);
  31.         assertTrue(a1.equals(a2));
  32.         assertTrue(Objects.equals(a1, a2));
  33.     }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment