1
public interface Validator {
static boolean checkEmptyOrNull(String string, ApiException apiException) {
    if (Objects.isNull(string) || string.trim().isEmpty()) {
        throw apiException;
    }
    return true;
}


static boolean checkEmptyOrNull(int value, ApiException apiException) {
    if (value < 1) {
        throw apiException;
    }

    return true;
}

}

Why haven't we just created a class instead of interface? What difference it would make?

  • 1
    You should ask the author of this code. There’s so much wrong with this code, it’s not worth discussing whether the interface could be a class. – Holger Jun 29 '22 at 13:28
  • Does that code execute? that is not an interface then. An interface is just function signatures and properties not code at ALL. – cbuteau Jul 02 '22 at 12:07
  • 1
    Consult with the code's author – java_coder Aug 28 '23 at 12:15

2 Answers2

1

The static methods was included in Java 8.

Suppose we have the Product interface which is implemented in multiple classes, for that case we could add product operations directly in the interface, this allows to group responsibilities and reuse code, just that.

Igor Román
  • 186
  • 1
  • 5
1

The static methods in interfaces are similar to default methods but the difference is that you can’t override them.

A link to an old question (pre Java 8) but it has been updated to include this answer https://stackoverflow.com/a/513001/2310289

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64