Commit 6af9ff2
committed
feat(interfaces): add explanation and examples for static methods in interfaces
What
- Documented the concept of **static methods in interfaces** (introduced in Java 8).
- Explained their behavior, declaration rules, and contrast with default methods.
- Added clear guidance on:
- How to declare (`static` keyword with a body).
- How to invoke (`InterfaceName.method()`).
- Why they cannot be overridden by implementing classes.
- How Java 9+ supports `private static` for internal helpers.
- Provided concise FAQ and example scenarios (e.g., A.util(), MyClass.util()).
Why
- Java interfaces before 8 could only declare abstract methods → no shared utility logic.
- With Java 8, static methods allow grouping related helpers *inside the API contract itself* (e.g., `Collections`, `Map.of()`).
- Improves discoverability: utility methods tied to an interface appear directly in IDE autocompletion.
- Clarifies key distinction:
- **Default methods** → instance-level, can be inherited/overridden.
- **Static methods** → interface-level, cannot be overridden or inherited.
Logic
1. **Declaration**
- Must have a body in the interface.
- Example:
```java
interface A {
static void util() { System.out.println("helper"); }
}
```
2. **Call site**
- Invoked using the interface name:
```java
A.util();
```
- Cannot be called via an instance (`obj.util()` → compile error).
3. **Non-overriding rule**
- If an implementing class defines a static method with the same signature, it is a separate method, not an override.
- Call resolution depends on qualifier (`InterfaceName.method()` vs `ClassName.method()`).
4. **Java 9+ enhancement**
- `private static` methods allowed for factoring out code used by multiple default/static methods.
- Keeps interfaces cleaner by hiding internal helpers.
5. **Inheritance model**
- Static methods do not participate in polymorphism (no super/virtual dispatch).
- They belong strictly to the interface declaration.
Real-life applications
- **API helpers**: `Comparator.comparing()`, `Map.of()`, `List.of()` are static factory methods that simplify usage.
- **Encapsulation**: interface groups both behavior contracts (abstract/default) and related utilities (static).
- **Consistency**: avoids scattering utility methods into unrelated classes (like `Utils` classes).
Notes
- Interface static methods are `public` by default, but can also be `private` (Java 9+).
- Cannot be abstract.
- Good practice: use static methods for factory methods, validators, or other utilities closely tied to the interface.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent a4b5867 commit 6af9ff2
File tree
1 file changed
+61
-0
lines changed- Java 8 Crash Course/Functional Interface/Static Methods/src
1 file changed
+61
-0
lines changedLines changed: 61 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
0 commit comments