An instance variable is a variable declared inside a class but outside any method, constructor, or block. It represents data that belongs to an individual object.
If you create ten objects from the same class, each object normally has its own separate copy of the instance variables. This makes instance variables fundamental to understanding how objects maintain their own state.
Why Do We Need Instance Variables?
Imagine a BankAccount class. Every account needs information such as an account number, account holder name, and balance.
class BankAccount {
String accountNumber;
String holderName;
double balance;
}
These variables describe the state of an individual bank account. If two account objects are created, each object should have its own account number, holder name, and balance.
BankAccount account1 = new BankAccount(); BankAccount account2 = new BankAccount(); account1.balance = 5000; account2.balance = 12000;
The balance of account1 is 5000, while the balance of account2 is 12000. Changing one object's instance variable does not automatically change the other object's variable.
Remember: Instance variables belong to objects. Each object normally maintains its own copy of every instance variable declared by its class.
Where Are Instance Variables Declared?
An instance variable is declared directly inside a class, but outside methods, constructors, and blocks.
class Employee {
String name;
int age;
double salary;
void display() {
System.out.println(name);
}
}
Here, name, age, and salary are instance variables because they are declared inside the class but outside the method.
Instance Variables Belong to Objects
Consider two employee objects:
Employee employee1 = new Employee(); Employee employee2 = new Employee(); employee1.name = "Riya"; employee2.name = "Arjun";
The class defines that an employee has a name, but the actual value belongs to each individual object.
| Object | name | salary |
|---|---|---|
| employee1 | Riya | 55000 |
| employee2 | Arjun | 62000 |
Both objects have the same class-defined structure, but their instance state can be different.
Instance Variables and Constructors
Constructors are commonly used to initialize instance variables when an object is created.
class Employee {
String name;
int id;
double salary;
Employee(String name, int id, double salary) {
this.name = name;
this.id = id;
this.salary = salary;
}
}
The constructor receives values through parameters and stores them in the current object's instance variables.
Employee employee = new Employee("Riya", 101, 55000);
After construction, that particular employee object contains its own initialized state.
Accessing Instance Variables
Instance variables can be accessed through an object reference when their access level permits it.
class Car {
String model;
int speed;
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.model = "Sedan";
car.speed = 80;
System.out.println(car.model);
System.out.println(car.speed);
}
}
The dot operator is used to access the fields of the object. In production code, direct access is often restricted using access modifiers and controlled through methods, which is part of encapsulation.
Default Values of Instance Variables
Unlike local variables, instance variables receive default values automatically when an object is created. You do not have to explicitly initialize every instance variable before accessing it.
| Data Type | Default Value |
|---|---|
| byte, short, int, long | 0 |
| float, double | 0.0 |
| char | \u0000 |
| boolean | false |
| Reference types | null |
For example:
class Student {
String name;
int age;
boolean active;
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
System.out.println(student.name);
System.out.println(student.age);
System.out.println(student.active);
}
}
The output reflects Java's default initialization for these instance variables.
Important: The automatic default initialization of instance variables does not apply to local variables. A local variable must be assigned a value before it is read.
Instance Variables vs Local Variables
Instance variables and local variables may look similar, but they have important differences.
| Instance Variable | Local Variable |
|---|---|
| Declared inside a class but outside methods, constructors, and blocks | Declared inside a method, constructor, or block |
| Belongs to an object | Belongs to the current method or block execution |
| Automatically receives a default value | Does not receive an automatic default value before use |
| Can have access modifiers | Cannot be declared with ordinary access modifiers such as public or private |
| Exists as part of the object's state | Exists only within its applicable scope |
Instance Variables vs Static Variables
One of the most important distinctions in Java is between instance variables and static variables.
class Employee {
String name;
static String company = "TechCorp";
}
Here, name is an instance variable, so every employee object can have a different name. The company variable is static, so it belongs to the class rather than to individual employee objects.
| Instance Variable | Static Variable |
|---|---|
| Belongs to an object | Belongs to the class |
| Each object has its own copy | Generally one shared class-level variable |
| Can be accessed through an object | Can be accessed through the class name |
| Represents object-specific state | Represents class-level shared state |
Static members are covered in detail in the next major chapter, so for now focus on one simple distinction: instance state belongs to individual objects; static state belongs to the class.
Using this with Instance Variables
The this keyword can explicitly refer to the current object's instance variables.
class Account {
double balance;
Account(double balance) {
this.balance = balance;
}
}
The left side, this.balance, refers to the instance variable. The right side, balance, refers to the constructor parameter.
Changing Instance State
Instance variables can change during an object's lifetime.
class BankAccount {
double balance;
void deposit(double amount) {
balance = balance + amount;
}
}
If an object starts with a balance of 5000 and receives a deposit of 2000, its instance state changes to 7000.
BankAccount account = new BankAccount(); account.balance = 5000; account.deposit(2000); System.out.println(account.balance);
This ability to maintain and modify object-specific state is one of the central reasons instance variables are important in OOP.
Instance Variables and Encapsulation
Although Java allows instance variables to be declared with different access levels, professional code commonly avoids exposing important internal state directly.
class BankAccount {
private double balance;
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public double getBalance() {
return balance;
}
}
Here, the balance instance variable is private. External code cannot directly change it. Instead, the class controls how the balance is modified through methods.
This is a practical example of encapsulation: the object controls access to its internal state.
Common Beginner Mistakes
- Thinking that one instance variable value is automatically shared by all objects.
- Confusing instance variables with local variables.
- Assuming local variables receive the same automatic default values as instance variables.
- Making important instance variables public without considering encapsulation.
- Confusing instance variables with static variables.
Best Practices
- Use meaningful names that clearly describe the object's state.
- Initialize important instance variables through constructors or controlled methods.
- Use appropriate access modifiers to protect object state.
- Keep validation close to the code responsible for changing important state.
- Use static variables only when the data genuinely belongs to the class rather than individual objects.
Interview Insights
A common interview question is: “What is an instance variable in Java?”
A strong answer is: An instance variable is a non-static field declared inside a class but outside methods, constructors, and blocks. It belongs to an object, and each object normally has its own copy of that variable.
Interviewers may also ask why instance variables have default values while local variables do not. The key distinction is that instance variables are part of an object's automatically initialized state, whereas local variables must be definitely assigned before they are read.
Quick Learning Check
Before moving to static members, make sure you can answer these questions:
- Where are instance variables declared?
- Why does each object normally have its own instance state?
- What are the default values of instance variables?
- How are instance variables different from local variables?
- How are instance variables different from static variables?
- Why is encapsulation commonly used with instance variables?
Final Takeaway
Instance variables represent the state of individual objects. They are declared inside a class but outside methods, constructors, and blocks, and each object normally maintains its own values for them. Understanding instance variables gives you a clear picture of how objects store state, why constructors are useful for initialization, how this refers to the current object's fields, and why instance state is different from class-level static state.
