Skip to content

Commit 9d4185f

Browse files
committed
feat: Illustrate global static variable usage with shared companyName across all employee instances [global-variable-sharing]
📌 Core Concept: Demonstrates how a static (global) variable is shared across all instances of a class, and how modifying it reflects across all objects. 🏢 Program Overview: - `Company` class: - Instance Variables: `employeeName`, `salary` – unique for each object. - Static Variable: `companyName` – shared across all instances. - Constructor initializes individual employee details. - `display()` method prints employee + company data. 🧪 Behavior: - Initially, all objects reflect `companyName = "Tech Corp"`. - After updating `Company.companyName = "Global Tech"`, the change reflects for all employee objects instantly. 📌 Key Features: - Shared class-level state with `static` keyword. - Demonstrates difference between object-specific vs shared attributes. ✅ Useful pattern for: - Application-wide constants - Branding/labels - Environment configuration across objects Signed-off-by: Somesh diwan <[email protected]>
1 parent be981d9 commit 9d4185f

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

Section10Methods/LocalandGlobalVariables/src/GlobalVariable.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
/* A global variable is shared across all instances of a class.
2-
It is declared as static so that it belongs to the class instead of an object.
3-
4-
Key Features:
5-
companyName is shared across all instances.
6-
Changing companyName for one object affects all objects.
7-
*/
8-
91
class Company {
102
private String employeeName; // Instance variable (belongs to object)
113
private double salary; // Instance variable
@@ -41,4 +33,12 @@ public static void main(String[] args) {
4133
emp1.display();
4234
emp2.display();
4335
}
44-
}
36+
}
37+
38+
/* A global variable is shared across all instances of a class.
39+
It is declared as static so that it belongs to the class instead of an object.
40+
41+
Key Features:
42+
companyName is shared across all instances.
43+
Changing companyName for one object affects all objects.
44+
*/

0 commit comments

Comments
 (0)