Java 8: When the use of Interface static methods becomes a bad practice?

Jesus Zavarce

From Java 8, we can have default methods and static methods in the interfaces.

The constant interface pattern is a poor use of interfaces known as Constant Interface Antipattern.

>Effective Java, Item 17 :

The constant interface pattern is a poor use of interfaces. That a class uses some constants internally is an implementation detail. Implementing a constant interface causes this implementation detail to leak into the class's exported API. It is of no consequence to the users of a class that the class implements a constant interface. In fact, it may even confuse them. Worse, it represents a commitment: if in a future release the class is modified so that it no longer needs to use the constants, it still must implement the interface to ensure binary compatibility. If a nonfinal class implements a constant interface, all of its subclasses will have their namespaces polluted by the constants in the interface.

There are several constant interfaces in the java platform libraries, such as java.io.ObjectStreamConstants. These interfaces should be regarded as anomalies and should not be emulated.

If the use of constant interfaces is a bad practice, when the use of interface static methods could becomes a bad practice ?

biziclop

The main problem with constant interfaces isn't the interface that contains a lot of constants.

It's when classes implement that interface just so they can access the constants easier:

public interface Foo {
    public static final int CONSTANT = 1;
}

public class NotOkay implements Foo {
    private int value = CONSTANT;
}

public class Okay {
    private int value = Foo.CONSTANT;
}

Class NotOkay not only creates a fake relationship between the interface Foo and itself (there really is nothing to implement there), but the constant values become part of its public API.

(This practice was a lot more common before static imports were introduced in Java 5.)

With static interface methods there is really no point implementing the interface because you can't access them in the same manner: static interface methods are not inherited.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java 8 ClassFormatException for interface with static methods, but only when using assertions

From Java

Java: when to use static methods

From Dev

Java 8: Interface with static methods instead of static util class

From Dev

Static methods in java interface

From Dev

Is it bad practice NOT to use private methods and functions in Python?

From Dev

Is exposing static methods in abstract classes considered good or bad practice

From Dev

Java best practice: Class with only static methods

From Dev

When to use static variables/methods and when to use instance variables/methods in Java?

From Dev

When to use static and class methods

From Dev

Java Generic Interface vs Generic Methods, and when to use one

From Java

Is it bad practice to use break to exit a loop in Java?

From Dev

Java - How to avoid static and default methods in interface

From Dev

Java static methods/fields in interface (again!)

From Dev

Java - How to avoid static and default methods in interface

From Dev

PHPUnit: Is it a bad practice to use multiple assertions on mocked methods in one test?

From Dev

PHPUnit: Is it a bad practice to use multiple assertions on mocked methods in one test?

From Dev

Why Is it Consider Bad Practice to Use Static Class In Python

From Dev

lambda with non-static methods in Java 8

From Dev

Static data in firebase is a bad practice?

From Dev

When should I use static methods?

From Dev

Static Methods ok to use when using parameters?

From Java

Why is "final" not allowed in Java 8 interface methods?

From Dev

what is the implicit declaration of interface methods in Java 8?

From Dev

Use of Functional Interface in Java 8

From Dev

Is it a bad practice to throw exceptions in java 8 optional filters

From Dev

Combining builder methods with service methods - bad practice?

From Dev

PHP: When to use Traits and when to use static methods?

From Dev

Labels in Java - bad practice?

From Dev

Is it bad practice to use Consumers as setters and Suppliers as getters in Java?

Related Related

  1. 1

    Java 8 ClassFormatException for interface with static methods, but only when using assertions

  2. 2

    Java: when to use static methods

  3. 3

    Java 8: Interface with static methods instead of static util class

  4. 4

    Static methods in java interface

  5. 5

    Is it bad practice NOT to use private methods and functions in Python?

  6. 6

    Is exposing static methods in abstract classes considered good or bad practice

  7. 7

    Java best practice: Class with only static methods

  8. 8

    When to use static variables/methods and when to use instance variables/methods in Java?

  9. 9

    When to use static and class methods

  10. 10

    Java Generic Interface vs Generic Methods, and when to use one

  11. 11

    Is it bad practice to use break to exit a loop in Java?

  12. 12

    Java - How to avoid static and default methods in interface

  13. 13

    Java static methods/fields in interface (again!)

  14. 14

    Java - How to avoid static and default methods in interface

  15. 15

    PHPUnit: Is it a bad practice to use multiple assertions on mocked methods in one test?

  16. 16

    PHPUnit: Is it a bad practice to use multiple assertions on mocked methods in one test?

  17. 17

    Why Is it Consider Bad Practice to Use Static Class In Python

  18. 18

    lambda with non-static methods in Java 8

  19. 19

    Static data in firebase is a bad practice?

  20. 20

    When should I use static methods?

  21. 21

    Static Methods ok to use when using parameters?

  22. 22

    Why is "final" not allowed in Java 8 interface methods?

  23. 23

    what is the implicit declaration of interface methods in Java 8?

  24. 24

    Use of Functional Interface in Java 8

  25. 25

    Is it a bad practice to throw exceptions in java 8 optional filters

  26. 26

    Combining builder methods with service methods - bad practice?

  27. 27

    PHP: When to use Traits and when to use static methods?

  28. 28

    Labels in Java - bad practice?

  29. 29

    Is it bad practice to use Consumers as setters and Suppliers as getters in Java?

HotTag

Archive