Commit 443a27a
committed
docs: Add practical applications and examples of global (static) variables in Java [global-static-use cases]
🔑 Key: global-static-use cases
🧠 Highlights:
- Shared Configuration: Use static variables for database URLs, API keys, or application-wide settings.
- Object Tracking: Track number of created instances using a static counter (`userCount`).
- Shared Resources: Manage memory, resource pools, or connection instances across the app.
- Constants: Define shared values like tax rates or interest rates (`Bank.interestRate`).
- Logging Control: Toggle logging globally with static flags (`Logger.isDebugMode`).
- Game Dev Use: Keep global game limits or shared scoreboard values (`MAX_PLAYERS`, `highestScore`).
- Singleton Pattern: Maintain a single instance via static reference and controlled access (`DatabaseConnection.getInstance()`).
📚 Purpose:
- Demonstrates how `static` variables support data sharing, optimization, and consistency across instances.
- Useful in scenarios where a value or instance must be consistent throughout the application.
✅ Covers both real-world applications and design pattern usage (e.g., Singleton).
Signed-off-by: Somesh diwan <[email protected]>1 parent 5c08416 commit 443a27a
File tree
1 file changed
+4
-2
lines changed- Section10Methods/LocalandGlobalVariables/src
1 file changed
+4
-2
lines changedLines changed: 4 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| |||
51 | 52 | | |
52 | 53 | | |
53 | 54 | | |
54 | | - | |
| 55 | + | |
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
| |||
62 | 63 | | |
63 | 64 | | |
64 | 65 | | |
65 | | - | |
| 66 | + | |
| 67 | + | |
0 commit comments