Commit f0161f8
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- Section16StaticFinal/src
2 files changed
+112
-33
lines changedThis file was deleted.
Lines changed: 112 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
0 commit comments