Skip to content

Commit b299f90

Browse files
committed
feat: Demonstrate use of instance variables with unique data per Student object [object-instance-variables]
📌 Core Concept: Showcases how instance variables store object-specific data and how each instance of a class maintains its own state. 🎓 Program Overview: - `Student` class: - Instance Variables: `name`, `gpa` – unique for every object. - Constructor initializes these per-student attributes. - `display()` method outputs student-specific information. - `InstanceVariable` class: - Creates two `Student` objects with different values. - Calls `display()` on each to demonstrate data encapsulation. 🧪 Behavior: - `s1` and `s2` have independent `name` and `gpa` values. - Modifying one object’s data has no effect on the other. 📌 Key Features: - Object-level encapsulation using instance variables. - Demonstrates per-instance memory allocation and behavior. ✅ Useful pattern for: - Modeling real-world entities with unique properties. - Understanding how object-oriented programming handles data separation. Signed-off-by: Somesh diwan <[email protected]>
1 parent 9d4185f commit b299f90

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

Section10Methods/LocalandGlobalVariables/src/InstanceVariable.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/* An instance variable belongs to an object and is created when the object is instantiated.
2-
Each object has its own copy of instance variables.
3-
*/
4-
51
class Student {
62
private String name; // Instance variable
73
private double gpa; // Instance variable
@@ -29,7 +25,11 @@ public static void main(String[] args) {
2925
}
3026
}
3127

32-
/* Key Features:
28+
/*
29+
An instance variable belongs to an object and is created when the object is instantiated.
30+
Each object has its own copy of instance variables.
31+
32+
Key Features:
3333
Each Student object has its own name and gpa.
3434
Changing values for one object does not affect the other.
35-
*/
35+
*/

0 commit comments

Comments
 (0)