The Stacks
UNIT 1: USING OBJECTS AND METHODS · TOPIC 1.12

1.12 Objects: Instances of Classes

A class is a blueprint; an object is one thing built from it. This is the mental model for the whole second half of the course.

What you need to know

  • A class defines a type: what data its objects hold (attributes, stored in instance variables) and what they can do (behaviors, defined by methods).
  • An object is an instance of a class — one specific thing with its own values for the attributes. "hello" and "world" are two String objects.
  • Objects are created with new (except String literals, which Java creates for you). Each new makes a separate object.
  • The class's methods are the same for every object; the instance variable values differ. Two Student objects share the getName() method but have different names.
  • Object-oriented programming models a program as objects interacting by calling each other's methods.
  • A class can be used without seeing its code — you only need its API. That's the point of 1.7.

Worked example

// Class: the blueprint (someone wrote this)
public class Dog
{
    private String name;
    private int age;
    // ... constructor and methods
}

// Objects: individual dogs built from the blueprint
Dog d1 = new Dog("Rex", 3);
Dog d2 = new Dog("Bo", 5);
// d1 and d2 are two instances with different attribute values
Exam tip: Class vs. object questions are definitional: class = template/type, object = one instance with specific values. "How many objects are created" = count the new keywords (plus string literals).

Going deeper

The nuance, edge cases, and connections that turn a 3 into a 5.

  • A class is a type you define: it says what data its objects hold and what operations they support. Objects are instances — each has its own copy of the instance variables with its own values, but all share the class's methods.
  • Attributes (state) become instance variables. Behaviors become methods. When the exam describes a real-world thing — a bank account with a balance that can be deposited to — you should be able to map nouns to attributes and verbs to behaviors.
  • Objects are created with new. Every new makes a distinct object, even with identical arguments. new Dog("Rex") twice creates two dogs named Rex.
  • String is a class, but String literals are created for you: "hello" is a String object without new. Also, identical literals may share one object — which is why == on Strings sometimes "works" and why you should never rely on it.
  • You can use a class without seeing its code. FRQ 3 gives you a class (say, Student) with its method headers and asks you to process a list of them. You never see the class body; you use the API.
  • Object-oriented design is thinking in terms of objects that hold state and send messages (method calls) to each other. The exam's Unit 3 is where you write classes; Unit 1 is where you learn to use them.

Mistakes that cost points

  • Counting variables as objects. Dog c = a; creates zero objects. Count the news.
  • Calling the blueprint an instance. Class = blueprint. Object = the thing built from it.
  • Assuming two objects with equal state are the same object. They aren't. == is false; .equals may be true if the class defines it that way.

Practice questions

Written in the style of the real exam. Try each one before revealing the answer.

Q1 Which of the following best describes the relationship between a class and an object?
  1. A A class is an instance of an object.
  2. B An object is an instance of a class, with its own attribute values.
  3. C A class and an object are the same thing.
  4. D An object contains multiple classes.
Show answer

Answer: B. The class is the blueprint; each object is one instance made from it.

Q2 How many objects are created by the following code?
Dog a = new Dog("Max", 2);
Dog b = new Dog("Max", 2);
Dog c = a;
  1. A 1
  2. B 2
  3. C 3
  4. D 0
Show answer

Answer: B. Two new statements create two objects. Line 3 copies a reference — c points to the same object as a.

Key vocabulary

Class
a template that defines the attributes and behaviors of a type of object
Object
a specific instance of a class
Attribute
data stored in an object, held in instance variables
Behavior
what an object can do, defined by its methods