Instance Variables and Methods in Java
Instance variables and methods belong to each object — every object gets its own copy.
Instance Variables
Declared inside a class but outside any method. Each object has its own copy.
class Employee {
String name; // instance variable
double salary; // instance variable
String department; // instance variable
}
Employee alice = new Employee();
alice.name = "Alice";
alice.salary = 95000;
Employee bob = new Employee();
bob.name = "Bob";
bob.salary = 78000;
// Changing alice's salary doesn't affect bob's
alice.salary = 100000;
System.out.println(bob.salary); // still 78000
Default Values
Instance variables are auto-initialized:
| Type | Default |
|---|---|
int, long |
0 |
double, float |
0.0 |
boolean |
false |
char |
'\u0000' |
Objects (String, etc.) |
null |
Instance Methods
Methods that operate on the object’s own data:
class Employee {
private String name;
private double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
// Instance method — uses 'this' object's data
public double calculateBonus() {
return salary * 0.10;
}
public void giveRaise(double percentage) {
this.salary += this.salary * (percentage / 100);
}
public String toString() {
return name + " ($" + salary + ")";
}
}
Employee alice = new Employee("Alice", 95000);
System.out.println(alice.calculateBonus()); // 9500.0
alice.giveRaise(5);
System.out.println(alice); // Alice ($99750.0)
Each call to calculateBonus() uses that specific object’s salary.
this — The Current Object
Inside an instance method, this refers to the object the method was called on:
class Employee {
private String name;
public void setName(String name) {
this.name = name; // this.name = field, name = parameter
}
public boolean isSameAs(Employee other) {
return this.name.equals(other.name); // compare this object with another
}
}
Instance vs Local Variables
| Instance Variable | Local Variable | |
|---|---|---|
| Declared in | Class body | Method body |
| Scope | Entire object | Method only |
| Default value | Yes (0, null, false) | None — must initialize |
| Lifetime | Object lifetime | Method execution |
| Access via | this.varName |
Directly |
class Demo {
int instanceVar = 10; // instance — has default
void method() {
int localVar; // local — NO default
// System.out.println(localVar); // ERROR: not initialized
localVar = 20;
System.out.println(instanceVar + localVar); // 30
}
}
Passing Objects to Methods
When you pass an object to a method, you pass the reference:
public static void promote(Employee emp) {
emp.giveRaise(15); // modifies the ORIGINAL object
}
Employee alice = new Employee("Alice", 95000);
promote(alice);
System.out.println(alice); // salary is now raised
Quick Exercises
- Create a
Counterclass with an instance variablecountand methodsincrement(),decrement(), andgetCount(). - Create two
Counterobjects. Increment one 5 times and the other 3 times. Print both counts. - Write a method that takes two
Employeeobjects and returns the one with the higher salary.
Summary
- Instance variables: per-object, auto-initialized, live with the object
- Instance methods: operate on the calling object’s data via
this - Local variables: per-method, must be initialized, die when method ends
- Object references are passed by value — methods can modify object state
What’s Next?
Some data belongs to the class itself, not individual objects. Let’s learn about Class Variables and Methods (static).