Commit 2c3343d
committed
feat: Track student info using instance and static variables in School class [school-static-instance-vars]
🔑 Key: school-static-instance-vars
🧠 Logic & Features:
- Created a `School` class that holds:
• `studentName` (instance variable) – unique for each object
• `rollNumber` (instance variable) – assigned incrementally per student using static counter
• `schoolName` (static variable) – shared across all students
• `totalStudents` (static variable) – keeps track of how many students have been created
- Constructor:
• Assigns name and auto-increments the roll number using `++totalStudents`.
- Methods:
• `display()` – non-static method to print individual student info
• `displayTotalStudents()` – static method to print total number of students created
📌 Key Concepts:
- ✅ **Instance Variables**:
• Belong to each object; different for each student.
- ✅ **Static (Global) Variables**:
• `schoolName` is shared and same for all.
• `totalStudents` is used as a counter to give unique roll numbers.
- ✅ **Static Methods**:
• Used when logic is related to the class itself, not specific to any object.
- ✅ **Constructor**:
• Helps auto-assign unique roll numbers using a global counter.
Signed-off-by: Somesh diwan <[email protected]>1 parent f7c53cd commit 2c3343d
1 file changed
+1
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | | - | |
| 44 | + | |
0 commit comments