Commit 505d921
committed
feat: Add notes on static keyword usage and restrictions in Java
WHAT the doc covers:
- **Purpose of static**:
- Used for memory management by creating class-level members.
- Belongs to the class itself, not tied to any particular object instance.
- Shared across all instances, reducing redundant copies of data.
- **Where static can be applied**:
- Methods → can be called without creating an object.
- Blocks → executed once during class loading for initialization.
- Nested classes → static nested classes don’t need an outer class instance.
- **Restrictions of static methods**:
- Cannot access non-static (instance) variables or methods directly, since they belong to objects.
- Cannot use `this` or `super`, because no specific object context exists in a static context.
- Can only:
- Access static members directly.
- Work with non-static members if an object reference is explicitly provided.
- **Concept summary**:
- Static members are directly attached to the class itself.
- Useful for utility functions, constants, shared resources, and singleton implementations.
WHY this matters:
- Clarifies why static is critical for efficient memory usage and consistent shared state.
- Helps avoid common beginner mistakes (e.g., trying to use `this` in a static method).
- Prepares for designing utility classes (`Math`, `Collections`) and managing application-wide data.
Short key: java static keyword memory-management restrictions no-this-or-super.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent da85251 commit 505d921
1 file changed
+16
-19
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | 1 | | |
11 | | - | |
12 | 2 | | |
13 | | - | |
| 3 | + | |
14 | 4 | | |
15 | | - | |
16 | | - | |
| 5 | + | |
17 | 6 | | |
18 | 7 | | |
19 | 8 | | |
| |||
48 | 37 | | |
49 | 38 | | |
50 | 39 | | |
51 | | - | |
52 | | - | |
| 40 | + | |
53 | 41 | | |
54 | 42 | | |
55 | 43 | | |
56 | | - | |
57 | | - | |
| 44 | + | |
58 | 45 | | |
59 | 46 | | |
60 | 47 | | |
61 | 48 | | |
62 | 49 | | |
63 | 50 | | |
64 | | - | |
65 | 51 | | |
66 | 52 | | |
67 | 53 | | |
| |||
72 | 58 | | |
73 | 59 | | |
74 | 60 | | |
75 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
0 commit comments