Skip to content

Commit 8e97ae0

Browse files
committed
feat: Demonstrate object attributes with multiple instances of Attributes class
WHAT the code does: - Defines class Attributes with two private instance variables: - int number - String word - In main(): - Creates two separate objects (object1 and object2). - Assigns different values to each object’s fields. - Prints selected attributes from each object to show independence. WHY this matters: - Shows how **objects in Java maintain their own copies of instance variables**. - Reinforces the difference between: - Class-level (static) variables: shared across all instances. - Instance variables: unique per object. - Demonstrates encapsulation in theory (though fields are directly accessed here). - Clarifies how multiple objects of the same class can hold different states. HOW it works: 1. object1 is created: - object1.number = 9 - object1.word = "Grade" 2. object2 is created: - object2.number = 2022 - object2.word = "Java" 3. Printing: - object1 prints its number (9). - object2 prints its word ("Java"). - Shows object independence: changing object2 does not affect object1. Tips and gotchas: - Fields are declared private but accessed directly in main() because it’s inside the same class. - Outside this class, direct access is not possible — you’d need getters and setters. - Prefer using constructors or setter methods to initialize attributes for better encapsulation. - Overriding toString() could simplify printing object state (e.g., `System.out.println(object1);`). - Avoid hardcoding values directly into fields; encapsulation is a better practice. Use-cases / analogies: - Think of a class as a blueprint (like a form), and objects as filled-in forms. - object1’s form: number=9, word="Grade". - object2’s form: number=2022, word="Java". - Real-world analogy: two smartphones of the same model, but one has different wallpaper and storage space used — same class, different state. Short key: java-oop object-attributes instance-variables encapsulation object-independence. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 7bb2c6b commit 8e97ae0

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
public class Attributes
2-
{
1+
public class Attributes {
32
private int number; //every 'Attributes' object has a number
43
private String word; //every 'Attributes' object has a word
54

6-
public static void main(String[] args)
7-
{
8-
//creating an object
5+
public static void main(String[] args) {
6+
//creating an object.
97
Attributes object1 = new Attributes();
10-
//assigning values to its attributes
8+
9+
//assigning values to its attributes.
1110
object1.number = 9;
1211
object1.word = "Grade";
1312

14-
//creating a different object with different attributes
13+
//creating a different object with different attributes.
1514
Attributes object2 = new Attributes();
1615
object2.number = 2022;
1716
object2.word = "Java";
1817

19-
//accessing values of attributes of objects
18+
//accessing values of attributes of objects.
2019
System.out.println("object1 has a number " + object1.number);
2120
System.out.println("object2 has a word " + object2.word);
2221
}
23-
}
22+
}

0 commit comments

Comments
 (0)