01
Overview

Instance variables: an object's private memory.

They're where each object remembers what makes it unique — and the AP exam tests their lifetime, visibility, and initialization closely.

An instance variable (also called a field) is a variable declared in the body of a class but outside any method. Every object built from the class gets its own independent copy of every instance variable.

Instance variables hold state: data that persists for the lifetime of the object. They are different from local variables (declared inside methods), which disappear when the method returns.

On the AP exam, instance variables are tested in tracing problems, in FRQ class design, and in MCQ questions about scope and default values.

02
Core Concept

Each object owns its own fields.

Same class, separate state.

Declaring instance variables

public class Dog {
    private String name;
    private int age;
}

Instance variables are declared inside the class body but outside any method. They should be private to support encapsulation.

Each object has its own copy

Dog a = new Dog();   // a has its own name and age
Dog b = new Dog();   // b has its own name and age
// changing a.name does not affect b.name

Default values

If you don't initialize an instance variable, Java assigns a default based on its type:

  • int, double0 or 0.0
  • booleanfalse
  • Object types (String, int[], etc.) → null

This is different from local variables, which have no default and must be assigned before use.

Lifetime and scope

Instance variables exist as long as their object exists. They are accessible from any method of the class. Local variables exist only inside the block where they are declared.

Accessing fields inside the class

public class Dog {
    private String name;

    public void setName(String n) {
        name = n;       // accesses the field directly
        // or: this.name = n;
    }
}

The keyword this refers to the current object. Use this.name when you need to distinguish the field from a parameter of the same name.

03
Key Vocabulary

Field terminology you'll see everywhere.

These terms appear in MCQ stems and FRQ rubrics throughout AP CSA.

Vocabulary
  • Instance variable — a variable declared in a class body, owned by each object.
  • Field — another name for an instance variable.
  • State — the current values of an object's instance variables.
  • Local variable — a variable declared inside a method; disappears when the method ends.
  • Default value — the value Java assigns to an uninitialized instance variable.
  • Scope — the region of code where a variable can be used.
  • this — keyword that refers to the current object.
  • Static variable — a class-level variable shared by all instances (rare in AP CSA scope).
04
AP Exam Focus

How instance variables are tested.

Visibility, defaults, and the difference from local variables matter most.

Exam Focus

MCQ patterns to expect:

  • Private field access from outside. Client code cannot read or write a private field directly; it must go through accessors.
  • Default values. An int field starts at 0; a reference field starts at null.
  • Shadowing. If a method has a parameter or local variable with the same name as a field, the local one wins inside that method. Use this to refer to the field.
  • State independence. Changes to one object's fields don't affect another object's fields.

FRQ tip: declare instance variables at the top of the class, all private, with names that match the prompt exactly.

05
Worked Example

Trace independent object state.

Two objects, two sets of fields, no interference.

Worked Example AP-style reasoning

Problem. Given the class below, what is printed?

public class Counter {
    private int count;

    public Counter() {
        count = 0;
    }

    public void increment() {
        count++;
    }

    public int getCount() {
        return count;
    }
}

// Client code:
Counter a = new Counter();
Counter b = new Counter();
a.increment();
a.increment();
b.increment();
System.out.println(a.getCount() + " " + b.getCount());

Step 1. new Counter() creates two separate objects. Each has its own count field, both initialized to 0.

Step 2. a.increment() twice — a.count becomes 2.

Step 3. b.increment() once — b.count becomes 1.

Step 4. Print a.getCount() and b.getCount(): 2 1.

Answer. The program prints 2 1.

Why this matters. Each object's fields are completely independent. If count were declared static, both objects would share one counter and the output would be 3 3.

06
Common Mistakes

Field errors students repeat on every exam.

Most come from confusing fields with local variables.

Watch Out
  • Re-declaring the field inside a method. Writing int count = 0; inside increment() creates a new local variable that shadows the field.
  • Assuming local variables have default values. They don't — Java requires explicit initialization before use.
  • Accessing fields with the dot on the same class. Inside the class, use fieldName or this.fieldName, not ClassName.fieldName.
  • Public fields. Almost never appropriate in AP CSA; rubrics expect private.
  • Using static by accident. A static field is shared across all objects, which usually breaks the intended design.
  • Naming a parameter the same as a field without using this — the parameter shadows the field.
07
Reference Table

Instance variables vs local variables.

The two most-confused categories of variables in Java.

Instance Variables Reference

AP Quick Reference
Feature Instance variable Local variable
Declared in Class body, outside methods Inside a method or block
Owned by Each object Each method call
Default value Yes (0, false, null) No — must be assigned
Lifetime As long as the object exists Until the block ends
Scope Whole class The enclosing block
Typical visibility private n/a
08
Practice Tip

How to think clearly about fields.

A diagram of each object's box of fields makes everything clearer.

Practice Strategy

Use the three practice tools below this lesson in order:

  • MCQ Practice — draw a labeled box for each object with its fields, then update field values as code runs. Solves almost every state-tracing problem.
  • Java Lab — define classes with two or three private fields and write methods that read and update them. Test independence by creating multiple objects and changing each separately.
  • FRQ Practice — copy field names from the prompt exactly. Always declare them private at the top of the class.

If you can predict each object's state at any point in a program, instance variables are no longer mysterious.

09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

Topic Quiz 30 min 30 marks Pending

AP CSA Topic Practice: Instance Variables

AP-style topic practice assessment

Start
FRQ Practice 30 min 18 marks Pending

AP CSA Unit 03 Topic 02 Instance Variables FRQ Practice

AP-style topic practice assessment

Start