File: InterfaceWithOnlyStatics.md

package info (click to toggle)
error-prone-java 2.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 23,204 kB
  • sloc: java: 222,992; xml: 1,319; sh: 25; makefile: 7
file content (26 lines) | stat: -rw-r--r-- 661 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Interfaces should be used to define types. Using an interface as a collection of
static methods and fields violates that, and can lead to confusing type
hierarchies if the interface is then implemented to allow easy access to the
constants.

Prefer using a `public final` class instead to prohibit subclassing.

```java
public interface Constants {
  final float PI = 3.14159f;
}
```

```java
public final class Constants {
  public static final float PI = 3.14159f;

  private Constants() {}
}
```

See
[Effective Java 3rd Edition ยง22: Use interfaces only to define types][ej3e-22]
for more details.

[ej3e-22]: https://books.google.com/books?id=BIpDDwAAQBAJ