Commit 2ed49cd
committed
feat: Demonstrate static vs local variable behavior across multiple method calls [static-local-scope]
🔑 Key: static-local-scope
🧠 Logic:
- `static int a = 10`: A class-level variable shared across all instances; retains value across method calls.
- `int b = 10`: A method-local variable, freshly created on each method call.
- Method `fun()` prints `a` and `b`, then increments both.
- `++a`: Affects the shared static value (persistent across calls).
- `++b`: Affects the temporary local value (discarded after each call).
📌 Observations:
- First call to `fun()` → prints `10 10`, then increments `a` to `11`.
- Second call → prints `11 10`, because `a` retained its value while `b` re-initialized.
🎯 Concept Reinforced:
- Difference between static and local variables:
- Static memory: One copy shared → used when persistence is needed.
- Local stack memory: Temporary → used for short-lived calculations or logic.
📚 Learning Use:
- Clarifies variable scope and lifetime.
- Useful for understanding memory model, especially in OOP design and interview questions.
Signed-off-by: Somesh diwan <[email protected]>1 parent 7a5d1e2 commit 2ed49cd
File tree
2 files changed
+33
-13
lines changed- Section10Methods/LocalandGlobalVariables/src
2 files changed
+33
-13
lines changedLines changed: 33 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 | + | |
Lines changed: 0 additions & 13 deletions
This file was deleted.
0 commit comments