Skip to content

Commit c26a7fe

Browse files
committed
feat: Add StaticAttributes class to demonstrate static vs instance variables.
WHAT the code does: - Defines a class `StaticAttributes` with: - **Static attribute** `word` (shared across all objects). - **Instance attribute** `number` (unique per object). - Constructor sets both `word` and `number`. - `displayInfo()` prints current values of `word` and `number`. - `changeWord()` is a static method to modify `word`. - `main()` demonstrates: 1. Accessing `word` directly via the class. 2. Creating objects (`object1`, `object2`) that update `word` each time. 3. Showing how all objects share the static `word`. 4. Using `changeWord()` to update `word` again and observing the change across all instances. WHY this matters: - Demonstrates **difference between static and instance fields**. - Shows how static members are shared across objects, while instance members remain unique. - Reinforces class-level vs object-level data handling. HOW it works: 1. Initially, `word = "ABC"`. 2. `object1 = new StaticAttributes("DEF", 2)` → `word = "DEF"`, `number = 2`. 3. `object2 = new StaticAttributes("GHI", 4)` → `word = "GHI"`, `number = 4`. - Since `word` is static, this update affects `object1` as well. 4. Calling `changeWord("JKL")` updates `word` for all objects. 5. Final prints show all objects reflecting the updated `word`. Tips & gotchas: - Static fields are shared → changing one changes all, which may cause unintended side effects. - For object-specific values, use instance variables only. - Static variables are often used for constants, counters, or utility/shared state. - Good naming conventions (e.g., uppercase for constants) improve readability. Use-cases: - Educational demonstration of **static vs instance behavior**. - Basis for counters (e.g., counting created objects). - Managing class-level configuration shared by all instances. - Useful in design patterns like Singleton or utility/helper classes. Short key: class-static attributes-shared-vs-instance. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 24d6acd commit c26a7fe

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

Section10Methods/Methods 2.O/src/StaticAttributes.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,43 @@
1-
public class StaticAttributes
2-
{
3-
//Static attributes//
4-
public static String word = "ABC"; //word is initially "ABC"
1+
public class StaticAttributes {
2+
// Static attributes.
3+
public static String word = "ABC";
54

6-
//Instance attributes//
5+
//Instance attributes.
76
public int number;
87

9-
//Constructor//
10-
public StaticAttributes(String a, int b)
11-
{
8+
//Constructor.
9+
public StaticAttributes(String a, int b) {
1210
word = a;
1311
number = b;
1412
}
1513

16-
//Instance method//
17-
public void displayInfo()
18-
{
14+
//Instance method.
15+
public void displayInfo() {
1916
System.out.println("Word is " + word + ", number is " + number);
2017
}
2118

22-
//Static method//
23-
public static void changeWord (String newWord)
24-
{
19+
//Static method.
20+
public static void changeWord(String newWord) {
2521
word = newWord;
2622
}
2723

28-
public static void main(String[] args)
29-
{
30-
//we don't need to create an object to look up the value of a static variable, the class itself can be used
24+
public static void main(String[] args) {
25+
//we don't need to create an object to look up the value of a static variable,
26+
//the class itself can be used.
3127
System.out.println(StaticAttributes.word);
3228

33-
//create the first object
29+
//create the first object.
3430
StaticAttributes object1 = new StaticAttributes("DEF", 2);
3531
object1.displayInfo();
3632

37-
//create a second object
33+
//create a second object.
3834
StaticAttributes object2 = new StaticAttributes("GHI", 4);
3935
object2.displayInfo();
4036

4137
//notice object1 has been modified!
4238
object1.displayInfo();
4339

44-
//now change the static variable
40+
//now change the static variable.
4541
changeWord("JKL");
4642

4743
//the objects have been modified once again!

0 commit comments

Comments
 (0)