Commit 0664a60
committed
feat: Add detailed documentation on
WHAT the doc covers:
- **Definition and purpose of static keyword**:
- Makes variables, methods, or blocks independent of object instances.
- Shared across all objects of a class.
- Example: `static int a = 5;` is accessible by all instances.
- **Where static can be used**:
- Static variables: single copy shared by all objects.
- Static blocks: executed once when the class is loaded.
- Static methods: callable without creating an object; limited to static members.
- Static nested classes: inner classes that can exist without an outer class instance.
- **Memory allocation**:
- Class loading process explained:
1. Compilation creates `.class` file.
2. JVM loads class with Class Loader System.
3. Static variables initialized and static blocks executed during class load.
- Static variables live in class-level memory, not per object, making them object-independent.
- **Static vs instance variables**:
- Static variables → belong to the class; changes are reflected across all objects.
- Instance variables → belong to each object; changes affect only the specific instance.
- Demonstrated with Calc example:
- Modifying obj1.stA changes it for obj2.
- Modifying obj1.inB doesn’t affect obj2’s inB.
- **Static vs non-static blocks**:
- Static block: executes once when class is loaded, before main().
- Non-static block: executes before constructor, every time a new object is created.
- Example with Calc and Help classes showing execution order.
- **Static vs non-static methods**:
- Static methods:
- Called with class name or object reference.
- Cannot access non-static members without an object.
- Useful for utility and helper methods.
- Non-static methods:
- Depend on object instance to be called.
- Can access both static and non-static members.
- Key rules:
- Non-static code can freely use static members.
- Static code cannot access non-static members without an object reference.
WHY this matters:
- Understanding static keyword is essential for:
- Designing efficient memory usage with shared data.
- Implementing utility classes (e.g., Math).
- Handling initialization logic correctly with static blocks.
- Avoiding confusion between class-level and object-level state.
Short key: java static keyword memory-allocation static-vs-instance methods blocks variables.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>static keyword usage, memory allocation, and behavior in Java1 parent 726dc37 commit 0664a60
1 file changed
+18
-15
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 | | - | |
8 | | - | |
9 | 7 | | |
| 8 | + | |
10 | 9 | | |
11 | 10 | | |
12 | 11 | | |
13 | 12 | | |
14 | 13 | | |
15 | | - | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
16 | 18 | | |
17 | 19 | | |
18 | 20 | | |
19 | | - | |
| 21 | + | |
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
| 26 | + | |
24 | 27 | | |
25 | 28 | | |
26 | 29 | | |
27 | 30 | | |
28 | 31 | | |
29 | 32 | | |
30 | 33 | | |
| 34 | + | |
31 | 35 | | |
32 | 36 | | |
33 | 37 | | |
| |||
37 | 41 | | |
38 | 42 | | |
39 | 43 | | |
| 44 | + | |
40 | 45 | | |
41 | 46 | | |
42 | 47 | | |
43 | 48 | | |
44 | 49 | | |
45 | 50 | | |
46 | 51 | | |
| 52 | + | |
47 | 53 | | |
48 | 54 | | |
49 | 55 | | |
| |||
55 | 61 | | |
56 | 62 | | |
57 | 63 | | |
58 | | - | |
| 64 | + | |
59 | 65 | | |
60 | 66 | | |
61 | 67 | | |
62 | 68 | | |
63 | 69 | | |
64 | 70 | | |
65 | | - | |
| 71 | + | |
66 | 72 | | |
67 | | - | |
| 73 | + | |
68 | 74 | | |
69 | 75 | | |
70 | 76 | | |
| |||
139 | 145 | | |
140 | 146 | | |
141 | 147 | | |
142 | | - | |
143 | 148 | | |
144 | 149 | | |
145 | 150 | | |
146 | 151 | | |
147 | 152 | | |
148 | 153 | | |
149 | 154 | | |
150 | | - | |
151 | | - | |
152 | 155 | | |
153 | 156 | | |
154 | 157 | | |
155 | | - | |
| 158 | + | |
156 | 159 | | |
157 | 160 | | |
158 | 161 | | |
| |||
161 | 164 | | |
162 | 165 | | |
163 | 166 | | |
164 | | - | |
| 167 | + | |
0 commit comments