Skip to main content
United StatesComputer ScienceSyllabus dot point

What is the relationship between a class and an object, and what does it mean to say an object is an instance of a class?

Topic 2.1 Objects: Instances of Classes: explain the relationship between a class and its objects, and describe an object as an instance of a class with state and behavior.

A focused answer to AP CSA Topic 2.1, covering the class-object relationship, what it means for an object to be an instance, the difference between attributes (state) and methods (behavior), and reference versus primitive variables, with a worked example.

Generated by Claude Opus 4.88 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. Class and object
  3. State and behavior
  4. Reference types
  5. Try this

What this topic is asking

The College Board (Topic 2.1) wants you to explain the relationship between a class and an object: a class is a blueprint, and an object is an instance built from it. You should describe an object in terms of its state (the data in its attributes) and its behavior (what its methods do), and recognize that an object variable is a reference type, unlike the primitives of Unit 1.

Class and object

Think of the class as the design and the object as a product made from that design. The class String is a blueprint; "hello" is one String object made from it. You can build many objects from one class, and each object is independent: changing one does not change another.

State and behavior

Every object has two aspects:

  • State - the data the object currently holds, stored in its attributes (also called instance variables or fields). Two objects of the same class can have different state.
  • Behavior - what the object can do, defined by its methods. Calling a method may read or change the object's state.

For a Rectangle class, the state might be a width and a height; the behavior might be a method that returns the area. Each Rectangle object has its own width and height (its state), but they all share the same set of methods (their behavior) defined by the class.

Reference types

This distinction matters throughout Unit 2. When you write String s = "hi";, the variable s does not contain the characters; it contains a reference to a String object that holds them. Understanding that object variables are references explains why two variables can refer to the same object, and why a freshly declared object variable can be null until you create an object for it (Topic 2.2).

Try this

Q1. State the difference between a class and an object. [2 points]

  • Cue. A class is a blueprint defining attributes and methods; an object is a specific instance created from that class, with its own attribute values.

Q2. Identify whether a String variable stores the characters directly or a reference to them, and name the category of type. [2 points]

  • Cue. It stores a reference to a String object, not the characters directly; String is a reference type.

Exam-style practice questions

Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AP 2021 (style)1 marksMultiple choice. Which statement best describes the relationship between a class and an object in Java? (A) A class is a single value, and an object is a copy of it. (B) A class is a blueprint that defines attributes and behaviors; an object is a specific instance created from that class. (C) An object defines the attributes, and a class is one instance of the object. (D) A class and an object are two names for the same thing. (E) An object can exist without any class.
Show worked answer →

The answer is (B).

A class is a blueprint or template that specifies what data (attributes) an object holds and what it can do (methods); an object is a concrete instance built from that blueprint, with its own values for the attributes. (C) reverses the roles. (A), (D) and (E) misstate the relationship - objects in Java are always created from a class, and many objects can be made from one class.

Markers reward identifying the class as the template and the object as an instance of it.

AP 2019 (style)3 marksFree response (short answer). A class `BankAccount` stores a balance and can deposit and withdraw money. (a) Using the terms class, object and instance, describe the relationship if `acct1` and `acct2` are both made from `BankAccount`. (b) Explain what is meant by the state and the behavior of an object.
Show worked answer →

A 3-point short-answer question testing object-oriented vocabulary.

(a) Point 1: BankAccount is the class (the blueprint); acct1 and acct2 are two separate objects, each an instance of the BankAccount class, with their own independent balances.

(b) Point 2: the state of an object is the data it currently holds in its attributes (here, the balance). Point 3: the behavior of an object is what it can do, defined by its methods (here, deposit and withdraw), which can change the state.

Markers reward correct use of class, object and instance, and distinguishing state (data) from behavior (methods).

Related dot points

Sources & how we know this