You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| Feature | Local Variable | Instance Variable |
|-------------------------- |--------------------------------------------------------|----------------------------------------------------------------------|
| Declaration | Declared inside a method or block | Declared inside a class |
| Scope | Only accessible within the method/block where declared | Accessible by all methods of the class (via object) |
| Memory Allocation. | Stored in stack memory | Stored in heap memory |
| Default Value | No default value, must be initialized before use | Default values (e.g., null, 0, false) |
| Lifetime. | Exists only during method execution | Exists as long as the object exists |
| Access Modifier. | No access modifier | Can have access modifiers (public, private, protected) |
| Storage Location | Stored in the stack | Stored in the heap (object) |
| Connection to Heap. | No connection to heap | Reference variable in the stack points to the object in the heap |
-------------------------------------------------------------------------------------------------------------------------------------------------------------
// Instance variables declared inside class - reference variable (name,age,address) is stored in stack and points to object (john,25,)in stackpublicclassPerson {
// Instance variables with different access modifiersprivateStringname = "John"; // Private instance variablepublicintage = 25; // Public instance variableprotectedStringaddress = "123 Main St"; // Protected instance variable// Method to display local variable - stored in stackpublicvoiddisplayInfo() {
// Local variable (no access modifier)// intx = 10; // local variable// Main method to run the programpublicstaticvoidmain(String[] args) {
// Creating an object of Person classPersonperson = newPerson();
}
}
+-------------------------------------------+ +-------------------------------------------+
| Stack - Local Variable | | Heap - Instance Variables |
+-------------------------------------------+ +-------------------------------------------+
| Reference variable: person -> 0x12345 | ---> | Object: Person |
| Local variable: x = 10 | | - name = "John" |
+-------------------------------------------+ | - age = 25 |
| - address = "123 Main St" |
+-------------------------------------------+
| (The reference 'person' in the stack |
| points to the memory address of the |
| object in the heap) |
+-------------------------------------------+
Key points
Local variables must be explicitly initialized before they can be accessed. If they are not initialized, trying to access them will lead to a compiler error.
Instance variables do not require initialization explicitly, they will be initialized to default values when the object is created.
intx; // Declaring local variable without initializingSystem.out.println(x); // Error: variable x might not have been initialized
(2) Constructors Vs Setters
Constructor
Setter
Special method with the SAME NAME AS CLASS
NORMAL METHOD follows camelCase naming.
Has NO RETURN TYPE
Has a return type (commonly void).
Called ONCE during object creation.
Can be called multiple times after object creation.
Used to INITIALIZE instance variables.
Used to UPDATE/CHANGE instance variables.
If only constructors are used → object is Immutable (values fixed after creation).
Using setters makes object Mutable (values can be updated).
Can be overloaded (multiple constructors with different parameters).
Setters are not overloaded, but multiple setters exist (one per variable).
Values are assigned only once when the object is created.
Values can be updated any number of times.
Constructor Example
classPerson {
Stringname;
Person(Stringname) {
this.name = name;
}
}
Personp1 = newPerson("John");
// Name fixed, cannot change later
Setter Example
classPerson {
privateStringname;
publicvoidsetName(Stringname) {
this.name = name;
}
}
Personp1 = newPerson();
p1.setName("John"); // can update later
(3) this Vs super Keyword
| Aspect | this | super
| ------------------------ | ------------------------------------------------------------------------------------------------------------------- |
| Meaning | Refers to the current object | Refers to the *parent (super) class* |
| Constructor Call | Used to call another constructor in the same class. | Used to call a constructor of the parent class. |
| Variable Access | Differentiates local variables from instance variables when they have same name | Accesses parent’s variables (if not private) |
| Method Access | Can call current class methods | Can call parent class methods (even if overridden) |
| Constructor Chaining. | Used for chaining within the same class | Used for chaining between child → parent |
| Restrictions | Must be the first statement in the constructor | Must be the first statement in the child constructor |
this
classStudent {
privateStringname;
// Constructor 1Student() {
this("Unknown"); // calls another constructor in SAME class
}
// Constructor 2Student(Stringname) {
this.name = name; // differentiates local var and instance var
}
voiddisplay() {
System.out.println("Name: " + name);
}
}
publicclassRunner {
publicstaticvoidmain(String[] args) {
Students1 = newStudent();
s1.display(); // Name: Unknown
}
}
When the compiler sees System.out.println(x) >> java first search whether a local variable x exists inside the current method?
If an x exists in the current method (display), that shadows everything else.
In the above example, int x = 99; will be printed for Case 1.
Second priority
If local variable is not present, java looks for Instance variable in the same class
Third priority → Parent class variable
If not found in current class, Java climbs up the inheritance chain to check the parent (Person).
That’s why this.x or super.x points to the value initialized in the parent constructor (10).
output
99 // local variable shadows everything
10 // parent's x (through this.x)
10 // parent's x (through super.x)
If Parent and Child Have Same Method - How to Call Parent’s One?