Commit 726dc37
committed
feat: Document differences between
WHAT the doc covers:
- **static keyword**
- Applies to: variables, methods, inner classes, and blocks.
- Purpose: binds the member to the class rather than to any specific object.
- Behavior:
- Static variables: shared across all instances of the class.
- Static methods: callable without object creation; can only access static members.
- Static blocks: executed once when the class is loaded, often used for initialization.
- Example:
```java
class Example {
static int count = 0; // shared across all instances
static void increment() { // can be called as Example.increment()
count++;
}
}
```
- **final keyword**
- Applies to: variables, methods, and classes.
- Purpose: makes something immutable or non-overridable.
- Behavior:
- Final variable: value cannot be reassigned once initialized.
- Final method: cannot be overridden by subclasses.
- Final class: cannot be extended.
- Example:
```java
final int MAX_VALUE = 100; // constant
final void show() { // cannot be overridden
System.out.println("Final method");
}
final class MyClass { // cannot be subclassed
// implementation
}
```
WHY this matters:
- `static` controls **scope and lifetime**, allowing memory-efficient shared state and utility methods.
- `final` enforces **immutability and stability**, preventing accidental changes, ensuring contracts, and increasing program safety.
- Both keywords are fundamental in designing reliable APIs, constants, utility classes, and secure inheritance hierarchies.
DIFFERENCES (key interview points):
1. **Purpose**:
- `static`: member belongs to the class itself.
- `final`: member cannot be changed/overridden/extended.
2. **Variables**:
- `static`: one shared copy for all objects.
- `final`: once assigned, cannot be reassigned.
3. **Methods**:
- `static`: invoked without an object; cannot use `this` or access instance members.
- `final`: cannot be overridden in subclasses.
4. **Classes**:
- `static`: denotes nested static class (accessed without outer instance).
- `final`: class cannot be subclassed.
Short key: java static-vs-final variables methods classes immutability class-level-members.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>static and final keywords in Java with variables, methods, and classes1 parent af362de commit 726dc37
1 file changed
+5
-9
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
5 | | - | |
6 | | - | |
| 6 | + | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
25 | | - | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
50 | 49 | | |
51 | 50 | | |
52 | 51 | | |
53 | 52 | | |
54 | | - | |
55 | 53 | | |
56 | 54 | | |
57 | 55 | | |
58 | 56 | | |
59 | | - | |
60 | 57 | | |
61 | 58 | | |
62 | 59 | | |
63 | 60 | | |
64 | | - | |
65 | 61 | | |
66 | | - | |
| 62 | + | |
0 commit comments