Bird
Raised Fist0
Javaprogramming~10 mins

Instance variables in Java - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Instance variables
Create Object
Allocate Memory for Instance Variables
Assign Default or Given Values
Use Instance Variables in Methods
Object Accesses Its Own Data
When an object is created, it gets its own copy of instance variables stored in memory, which methods can use and change.
Execution Sample
Java
class Car {
  String color; // instance variable
  void paint(String newColor) {
    color = newColor;
  }
}
Car myCar = new Car();
myCar.paint("red");
This code creates a Car object, then changes its color instance variable to "red" using a method.
Execution Table
StepActionInstance Variable 'color'Output/Notes
1Create Car object myCarnull (default)Object created, color is null by default
2Call myCar.paint("red")Set color to "red"color changed to red
3Access myCar.color"red"color is now red
4End"red"No more actions
💡 No more code to execute, instance variable 'color' holds "red"
Variable Tracker
VariableStartAfter Step 1After Step 2Final
myCar.colorundefinednull"red""red"
Key Moments - 2 Insights
Why is the instance variable 'color' null before calling paint()?
Because when the object is created (see step 1 in execution_table), instance variables get default values (null for String) until assigned.
Does changing 'color' inside paint() affect all Car objects?
No, it only changes the 'color' of the specific object calling paint(), as instance variables belong to each object separately.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what happens to 'color'?
AIt remains null
BIt is set to "red"
CIt is deleted
DIt becomes an integer
💡 Hint
Check the 'Instance Variable color' column at step 2 in execution_table
At which step does 'color' first get a non-null value?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Instance Variable color' column in execution_table
If we create another Car object without calling paint(), what is its 'color' value?
Anull
B"default"
C"red"
Dempty string ""
💡 Hint
Instance variables get default values on object creation as shown in step 1
Concept Snapshot
Instance variables belong to objects.
Each object has its own copy.
They get default values when object is created.
Methods can change them.
Changes affect only that object.
Full Transcript
Instance variables are variables that belong to each object created from a class. When you create an object, Java allocates memory for these variables and sets them to default values like null for Strings. You can change these variables by calling methods on the object. Each object keeps its own copy, so changing one object's instance variable does not affect others. For example, creating a Car object sets its color to null. Calling paint("red") on it changes its color to red. This change is stored inside that object only.

Practice

(1/5)
1. Which statement best describes instance variables in Java?
easy
A. They store data unique to each object of a class.
B. They are shared by all objects of a class.
C. They are declared inside methods only.
D. They must be static to be used.

Solution

  1. Step 1: Understand instance variable location

    Instance variables are declared inside a class but outside any method.
  2. Step 2: Understand instance variable behavior

    Each object has its own copy, so data is unique per object.
  3. Final Answer:

    They store data unique to each object of a class. -> Option A
  4. Quick 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
A. static int count;
B. int count() { }
C. public int count;
D. void count;

Solution

  1. 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;.
  2. Step 2: Eliminate invalid options

    int count() { } is a method, C is static (not instance), D is invalid syntax.
  3. Final Answer:

    public int count; -> Option C
  4. Quick 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
A. Compilation error
B. Blue
C. null
D. Red

Solution

  1. Step 1: Understand instance variable values per object

    Each Car object has its own color. c1.color is "Red" initially.
  2. Step 2: Check changes to c2.color

    Changing c2.color to "Blue" does not affect c1.color.
  3. Final Answer:

    Red -> Option D
  4. Quick 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
A. Instance variable 'name' is not assigned correctly in setName method.
B. Missing return type for setName method.
C. Instance variables must be static.
D. Class Person must have a constructor.

Solution

  1. Step 1: Analyze setName method parameter and assignment

    The method parameter name shadows the instance variable name.
  2. Step 2: Understand assignment effect

    Assignment name = name; assigns parameter to itself, not instance variable.
  3. Final Answer:

    Instance variable 'name' is not assigned correctly in setName method. -> Option A
  4. Quick Check:

    Use this.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
A. Static variables with constructor assigning values (shared by all).
B. Instance variables with constructor assigning unique values.
C. Instance variables with default constructor (no unique values).
D. Static variables with default constructor (shared and default).

Solution

  1. Step 1: Identify instance vs static variables

    Instance variables allow each object to have unique data; static variables share data across all objects.
  2. 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.
  3. Final Answer:

    Instance variables with constructor assigning unique values. -> Option B
  4. Quick 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