AP® Computer Science A · Unit 1 · Topic 1.12
Classes as Blueprints, Objects as Instances
A class is a blueprint that describes what fields and methods its objects will have. Each object built from that class is a separate instance with its own copy of those fields.
A class is a description, not a thing. It lists the instance variables every object of that type will carry, and the methods every object can call. Write class Student with fields name and score, and you have written a pattern, not a student. Nothing exists in memory yet.
Memory shows up when you call new. Each new Student(...) builds one object on the heap with its own name and score, independent of every other Student object. Build three students — say Ana with score 92, Ben with score 78, Carlos with score 85 — and you get three separate blocks of memory, each holding its own pair of values. Change Ben's score and Ana's is untouched, because they are not sharing storage, only sharing a design.
This is why the class itself can't hold instance data: it's the plan for the fields, not a place to store a value in them. You can't write Student.score = 92 for a non-static field, because score only comes into existence inside an object once new runs. This same split explains why every String you ever create can call substring — the String class wrote that method once, and every String object, no matter its characters, inherits the ability to use it on its own data.
Key terms
- class
- A blueprint that defines the instance variables and methods its objects will have.
- object
- A specific instance built from a class, with its own copy of the class's instance variables.
- instance variable
- A field declared in a class that gets a separate copy of storage inside every object of that class.
- new
- The operator that builds an object from a class, creating memory for its instance variables on the heap.
- heap
- The region of memory where all objects created with new actually live.
Watch it explained
- How it worksHow does a class describe attributes and behaviors that every one of its objects then has?
- How it worksWalk me through what exists in memory after three objects are built from one class.
- Common mistakeWhy isn't the class itself an object you can store data inside?
- Ties togetherHow does the blueprint idea explain why every String shares the same substring method?
Check yourself
End of topic.