Commit c26a7fe
committed
feat: Add StaticAttributes class to demonstrate static vs instance variables.
WHAT the code does:
- Defines a class `StaticAttributes` with:
- **Static attribute** `word` (shared across all objects).
- **Instance attribute** `number` (unique per object).
- Constructor sets both `word` and `number`.
- `displayInfo()` prints current values of `word` and `number`.
- `changeWord()` is a static method to modify `word`.
- `main()` demonstrates:
1. Accessing `word` directly via the class.
2. Creating objects (`object1`, `object2`) that update `word` each time.
3. Showing how all objects share the static `word`.
4. Using `changeWord()` to update `word` again and observing the change across all instances.
WHY this matters:
- Demonstrates **difference between static and instance fields**.
- Shows how static members are shared across objects, while instance members remain unique.
- Reinforces class-level vs object-level data handling.
HOW it works:
1. Initially, `word = "ABC"`.
2. `object1 = new StaticAttributes("DEF", 2)` → `word = "DEF"`, `number = 2`.
3. `object2 = new StaticAttributes("GHI", 4)` → `word = "GHI"`, `number = 4`.
- Since `word` is static, this update affects `object1` as well.
4. Calling `changeWord("JKL")` updates `word` for all objects.
5. Final prints show all objects reflecting the updated `word`.
Tips & gotchas:
- Static fields are shared → changing one changes all, which may cause unintended side effects.
- For object-specific values, use instance variables only.
- Static variables are often used for constants, counters, or utility/shared state.
- Good naming conventions (e.g., uppercase for constants) improve readability.
Use-cases:
- Educational demonstration of **static vs instance behavior**.
- Basis for counters (e.g., counting created objects).
- Managing class-level configuration shared by all instances.
- Useful in design patterns like Singleton or utility/helper classes.
Short key: class-static attributes-shared-vs-instance.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 24d6acd commit c26a7fe
1 file changed
+16
-20
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
5 | 4 | | |
6 | | - | |
| 5 | + | |
7 | 6 | | |
8 | 7 | | |
9 | | - | |
10 | | - | |
11 | | - | |
| 8 | + | |
| 9 | + | |
12 | 10 | | |
13 | 11 | | |
14 | 12 | | |
15 | 13 | | |
16 | | - | |
17 | | - | |
18 | | - | |
| 14 | + | |
| 15 | + | |
19 | 16 | | |
20 | 17 | | |
21 | 18 | | |
22 | | - | |
23 | | - | |
24 | | - | |
| 19 | + | |
| 20 | + | |
25 | 21 | | |
26 | 22 | | |
27 | 23 | | |
28 | | - | |
29 | | - | |
30 | | - | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
31 | 27 | | |
32 | 28 | | |
33 | | - | |
| 29 | + | |
34 | 30 | | |
35 | 31 | | |
36 | 32 | | |
37 | | - | |
| 33 | + | |
38 | 34 | | |
39 | 35 | | |
40 | 36 | | |
41 | 37 | | |
42 | 38 | | |
43 | 39 | | |
44 | | - | |
| 40 | + | |
45 | 41 | | |
46 | 42 | | |
47 | 43 | | |
| |||
0 commit comments