Skip to content

Commit 443a27a

Browse files
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

1 file changed

+4
-2
lines changed

Section10Methods/LocalandGlobalVariables/src/Applications Of Global Variable.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class User {
2020
userCount++; // Increase count every time an object is created
2121
}
2222
}
23+
2324
This helps in monitoring the total number of active users in a system.
2425

2526
3. Managing Shared Resources
@@ -51,7 +52,7 @@ Global Scoreboard (static int highestScore;)
5152
Example: Singleton Design Pattern (Ensuring only one instance of a class is created).
5253

5354
class DatabaseConnection {
54-
private static DatabaseConnection instance; // Global shared instance
55+
private static DatabaseConnection instance; // Global shared instance
5556

5657
private DatabaseConnection() {} // Private constructor to prevent multiple instances
5758

@@ -62,4 +63,5 @@ class DatabaseConnection {
6263
return instance;
6364
}
6465
}
65-
Used in databases to prevent multiple connections from being created.
66+
67+
Used in databases to prevent multiple connections from being created.

0 commit comments

Comments
 (0)