Instance variables store information unique to each object created from a class. They help keep data separate for each object.
Instance variables in Java
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Java
class ClassName { dataType variableName; // instance variable }
Instance variables are declared inside a class but outside any method.
Each object of the class has its own copy of instance variables.
Examples
Java
class Car { String color; // instance variable int year; }
Java
class Person { String name; int age; }
Sample Program
This program creates two Dog objects. Each dog has its own name and age stored in instance variables. The display method prints these details.
Java
class Dog { String name; // instance variable int age; void display() { System.out.println("Name: " + name); System.out.println("Age: " + age); } } public class Main { public static void main(String[] args) { Dog dog1 = new Dog(); dog1.name = "Buddy"; dog1.age = 3; Dog dog2 = new Dog(); dog2.name = "Max"; dog2.age = 5; dog1.display(); dog2.display(); } }
Important Notes
Instance variables get default values if not initialized (e.g., 0 for int, null for objects).
They belong to objects, so you access them through object references.
Use instance variables to keep data that varies from one object to another.
Summary
Instance variables hold data unique to each object.
They are declared inside a class but outside methods.
Each object has its own copy of instance variables.
Practice
1. Which statement best describes
instance variables in Java?easy
Solution
Step 1: Understand instance variable location
Instance variables are declared inside a class but outside any method.Step 2: Understand instance variable behavior
Each object has its own copy, so data is unique per object.Final Answer:
They store data unique to each object of a class. -> Option AQuick Check:
Instance variables = unique per object [OK]
Hint: Instance variables belong to objects, not the class itself [OK]
Common Mistakes:
- Confusing instance variables with static variables
- Thinking instance variables are declared inside methods
- Assuming instance variables are shared across all objects
2. Which of the following is the correct way to declare an instance variable in Java?
easy
Solution
Step 1: Identify valid instance variable syntax
Instance variables are declared like normal variables inside a class but outside methods, e.g.,public int count;.Step 2: Eliminate invalid options
int count() { } is a method, C is static (not instance), D is invalid syntax.Final Answer:
public int count; -> Option CQuick Check:
Instance variable declaration = variable with type and name [OK]
Hint: Instance variables look like normal variable declarations outside methods [OK]
Common Mistakes:
- Using parentheses which define methods, not variables
- Adding static keyword which makes variable class-level
- Missing type or using invalid syntax
3. What will be the output of this Java code?
class Car {
String color = "Red";
}
public class Test {
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car();
c2.color = "Blue";
System.out.println(c1.color);
}
}medium
Solution
Step 1: Understand instance variable values per object
Each Car object has its owncolor. c1.color is "Red" initially.Step 2: Check changes to c2.color
Changing c2.color to "Blue" does not affect c1.color.Final Answer:
Red -> Option DQuick Check:
Instance variables are unique per object [OK]
Hint: Changing one object's instance variable doesn't affect others [OK]
Common Mistakes:
- Assuming changing c2.color changes c1.color
- Confusing instance variables with static variables
- Expecting null because of misunderstanding initialization
4. Find the error in this Java class related to instance variables:
public class Person {
String name;
int age;
public void setName(String name) {
name = name;
}
}medium
Solution
Step 1: Analyze setName method parameter and assignment
The method parameternameshadows the instance variablename.Step 2: Understand assignment effect
Assignmentname = name;assigns parameter to itself, not instance variable.Final Answer:
Instance variable 'name' is not assigned correctly in setName method. -> Option AQuick Check:
Usethis.name = name;to assign instance variable [OK]
Hint: Use 'this.' to refer to instance variables inside methods [OK]
Common Mistakes:
- Not using 'this' to distinguish instance variables
- Assuming parameter assignment updates instance variable
- Thinking constructor is mandatory for instance variables
5. You want to create a class
Book where each book has a unique title and author. Which code correctly uses instance variables to achieve this?public class Book {
// Choose the correct instance variable declarations and constructor
A) String title, author;
public Book(String t, String a) {
title = t;
author = a;
}
B) static String title, author;
public Book(String t, String a) {
title = t;
author = a;
}
C) String title, author;
public Book() {
title = "";
author = "";
}
D) static String title, author;
public Book() {
title = "";
author = "";
}hard
Solution
Step 1: Identify instance vs static variables
Instance variables allow each object to have unique data; static variables share data across all objects.Step 2: Check constructor usage
Instance variables with constructor assigning unique values. uses instance variables with a constructor that assigns unique values from parameters, matching the requirement.Final Answer:
Instance variables with constructor assigning unique values. -> Option BQuick Check:
Use instance variables + constructor for unique object data [OK]
Hint: Use instance variables with constructor to set unique object data [OK]
Common Mistakes:
- Using static variables which share data across all objects
- Not initializing instance variables with constructor parameters
- Assuming default constructor sets unique values
