Skip to content

Commit f0161f8

Browse files
committed
feat: Document differences between references, objects, instances, and static vs instance members in Java
WHAT the doc covers: - Clarifies the difference between **reference variables**, **objects**, and **instances**: - Reference (lives on stack) → pointer to an object (like a remote). - Object (lives on heap) → actual data structure created by `new`. - Instance → a specific realization of a class (object). - Class → blueprint used to create instances. - Demonstrates with `Person p1 = new Person("Alice");` and `Person p2 = new Person("Bob");` where references point to separate objects but share the same class definition. - Explains **static variables (class variables)**: - Shared across all objects. - Stored in method area memory. - Used for counters, unique IDs, constants, and shared resources. - Example: `Counter.count` increments for each object created. - Explains **instance variables**: - Each object has its own copy. - Modifying one object’s variable does not affect others. - Example: `Person.name` is unique, but `Person.population` is shared. - Differentiates **static vs instance methods**: - Static methods: - Belong to class, not objects. - Called via class name (`Math.sqrt(25)`). - Cannot access instance variables, `this`, or `super`. - Can use static variables or operate on objects passed as parameters. - Instance methods: - Belong to objects. - Called via instance reference (`p1.sayHello()`). - Can access both instance and static members. WHY this matters: - Understanding references, objects, and instances helps avoid confusion about memory usage, object lifecycles, and variable scope. - Knowing static vs instance clarifies what belongs to the entire class vs each object, a key concept in Java’s object-oriented design. - Provides clarity for interviews, debugging memory issues, and writing clean, maintainable code. Short key: java-references objects instances static-vs-instance variables methods memory. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 1da6078 commit f0161f8

File tree

2 files changed

+112
-33
lines changed

2 files changed

+112
-33
lines changed

Section16StaticFinal/src/Note.txt

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
References vs Objects vs Instances, and then static vs instance members (variables and methods).
2+
3+
4+
5+
References vs Objects vs Instances:
6+
7+
Person p1 = new Person();
8+
9+
• p1 → This is a reference variable.
10+
11+
It lives on the stack (usually) and holds the memory address of the object created. It’s like the remote control to your TV.
12+
• new Person() → This creates an object in heap memory.
13+
The object contains all the instance variables defined in the Person class. Each new keyword call makes a brand new object.
14+
• Instance → An instance just means “an object of a class.” When we say “p1 is an instance of Person,” we mean it’s one
15+
specific realization of the class Person.
16+
• Class → Think blueprint. Objects are the houses built from the blueprint.
17+
18+
A single class can be used to create many instances (objects), each with its own state.
19+
20+
Example:
21+
22+
Person p1 = new Person("Alice");
23+
Person p2 = new Person("Bob");
24+
25+
• Both p1 and p2 are references.
26+
• Each reference points to its own object instance (Alice and Bob).
27+
• Both objects come from the same class definition.
28+
29+
30+
31+
Static Variables (a.k.a. Class Variables)
32+
33+
class Counter {
34+
static int count = 0;
35+
Counter() {
36+
count++;
37+
}
38+
}
39+
40+
• Shared across all instances. No matter how many objects you create, only one copy of count exists.
41+
• Accessed via Counter.count, though you can technically use obj.count (not recommended).
42+
• They exist in method area memory (class-level memory), not per object.
43+
44+
Use Cases:
45+
1. Counters / tracking number of objects
46+
2. Unique IDs
47+
3. Constants (like Math.PI)
48+
4. Shared resources (like a cached configuration or a singleton instance)
49+
50+
51+
52+
Instance Variables
53+
• Declared without static.
54+
• Each object (instance) gets its own separate copy.
55+
• Changing one instance variable doesn’t affect another object.
56+
57+
Example:
58+
59+
class Person {
60+
String name; // instance variable
61+
static int population = 0; // class variable
62+
63+
Person(String name) {
64+
this.name = name;
65+
population++;
66+
}
67+
}
68+
69+
• Every Person has its own name.
70+
• All Person objects share the population count.
71+
72+
73+
74+
Static Methods vs Instance Methods:
75+
76+
Static Methods
77+
• Declared with static.
78+
• Belong to the class itself, not any instance.
79+
• Can be called with the class name:
80+
Math.sqrt(25);
81+
• Restrictions:
82+
• Cannot access instance variables directly (they don’t know which object you mean).
83+
• Cannot use this or super (no current object context).
84+
• They can only:
85+
• Use static variables and call static methods directly.
86+
• Work with instance stuff only if you pass an object reference to them.
87+
88+
Example:
89+
90+
class MathUtil {
91+
static int square(int x) {
92+
return x * x;
93+
}
94+
}
95+
96+
Instance Methods
97+
• Belong to an object.
98+
• Require an instance to call:
99+
p1.sayHello();
100+
• Can access:
101+
• Both instance variables (through this)
102+
• And static variables (since they’re global to the class)
103+
104+
105+
Key Idea
106+
• Static = belongs to the class.
107+
• Instance = belongs to the object.
108+
109+
Or:
110+
The blueprint (class) has some things that are universal (static), and others that are unique to each house (instance).
111+
112+

0 commit comments

Comments
 (0)