Commit f7c53cd
committed
feat: Demonstrate static variables and static methods using Mobile1 class [static-method-variable-demo]
🔑 Key: static-method-variable-demo
🧠 Logic:
- `Mobile1.name` is a static variable → shared by all instances of the class.
- `brand` and `price` are instance variables → unique for each object.
- `show()` is a non-static method → accesses both instance and static variables.
- `show1()` is a static method → can only access static members directly but can use object reference to access instance members.
📌 Concepts Illustrated:
- 📌 **Static Variable**:
• Declared as `static String name;`
• Shared among all instances. Changes to `Mobile1.name` reflect in all objects.
• Example: Changing `Mobile1.name = "SmartPhone";` affects both `obj1` and `obj2`.
- 📌 **Instance Variables**:
• `brand` and `price` are specific to each object.
- 📌 **Static Method**:
• Can’t access non-static variables directly (e.g., `brand`, `price`).
• Accepts an object as parameter (`Mobile1 obj`) to access instance members.
🧪 Output:
- obj1: Apple : 1500 : SmartPhone
- obj2: Samsung : 1700 : SmartPhone
- show1(obj1): Apple : 1500 : SmartPhone
🧠 Note:
- If `name` were non-static, each object could have a different name.
- Static methods are typically used for utility or class-wide operations, not tied to object state.
Signed-off-by: Somesh diwan <[email protected]>1 parent 2ed49cd commit f7c53cd
File tree
1 file changed
+3
-5
lines changed- Section10Methods/LocalandGlobalVariables/src
1 file changed
+3
-5
lines changedLines changed: 3 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
13 | | - | |
| 12 | + | |
14 | 13 | | |
15 | 14 | | |
16 | 15 | | |
17 | 16 | | |
18 | 17 | | |
19 | 18 | | |
20 | | - | |
21 | | - | |
| 19 | + | |
22 | 20 | | |
23 | 21 | | |
24 | 22 | | |
| |||
41 | 39 | | |
42 | 40 | | |
43 | 41 | | |
44 | | - | |
| 42 | + | |
0 commit comments