Unit 3: Class Creation
A plain-language study guide to AP Computer Science A Unit 3, Class Creation: all 42 ideas across 9 topics, each explained for students.
Everything the AP Computer Science A exam can ask about in Unit 3, explained in plain language. This unit is roughly 10–18% of the multiple-choice section and has 9 topics and 42 ideas.
Each idea has a code like 1.2.A.1: the unit, the topic, the learning objective (the letter), then the idea itself.
The codes on the practice questions link straight to the idea they tested. These are summaries in our own words.
For the exact official wording, see the
AP Computer Science A Course and Exam Description.
Topics in this unit
- 3.1 Abstraction and Program Design
- 3.2 Impact of Program Design
- 3.3 Anatomy of a Class
- 3.4 Constructors
- 3.5 Methods: How to Write Them
- 3.6 Methods: Passing and Returning References of an Object
- 3.7 Class Variables and Methods
- 3.8 Scope and Access
- 3.9 this Keyword
Topic 3.1: Abstraction and Program Design
Practice this topic: Topics 3.1–3.2: Abstraction and Program Design
3.1.A — Plan a program's design in words or diagrams, showing each class with its data (attributes) and the actions (behaviors) it can perform.
- 3.1.A.1 Abstraction means making something simpler by focusing on the big idea. You hide the details that do not matter right now and group the useful, related pieces together, so there is less to keep in your head.
- 3.1.A.2 Data abstraction lets you use a piece of data by its name and meaning without worrying about how it is stored inside. The data can be one variable or a bigger bundle, such as an object made from a class.
-
3.1.A.3 An attribute is a piece of data that a class stores, declared in the class but outside every method and constructor.
- An instance variable is an attribute where each object has its own value.
- A class variable is an attribute that every object of the class shares.
- 3.1.A.4 Procedural abstraction gives a process a name, so you can call a method knowing what it does without knowing how. Method decomposition splits a big job into smaller methods, one per small job. Shared steps can go in one method instead of being copied, which lets you reuse code and keeps things simpler.
- 3.1.A.5 Parameters make a method flexible. Instead of doing one fixed thing, the method can work with many different input values (arguments) each time you call it.
- 3.1.A.6 You can rewrite what happens inside a method, for example to make it faster or use less memory, and nobody who calls it has to change anything. This works as long as the method signature and the job it does stay the same.
- 3.1.A.7 Before you write a class, plan it out: decide what data it holds (attributes) and what it can do (behaviors). You can write this plan in plain sentences or draw it as a diagram.
Topic 3.2: Impact of Program Design
Practice this topic: Topics 3.1–3.2: Abstraction and Program Design
3.2.A — Explain how computing systems affect society and raise ethical questions, including reliability, harm, and ownership of code.
- 3.2.A.1 A reliable system does its job correctly under the conditions it was built for, without failing. To make programs more reliable, test them with many different situations, not just one.
- 3.2.A.2 Programs affect people, money, and culture, in good ways and bad ways. Even a program built to solve a real problem can cause harm that its creators never planned for.
- 3.2.A.3 Writing programs raises legal questions about who owns code. Open source code is published for anyone to use freely, so programmers often reuse it. Code that is not open source needs the owner's permission first, and often you must pay for it.
Topic 3.3: Anatomy of a Class
Practice this topic: Topics 3.3–3.4: Anatomy of a Class and Constructors
3.3.A — Write code that controls who can use classes, variables, constructors, and methods by choosing public or private.
-
3.3.A.1 Data encapsulation means keeping a class's inner workings hidden from other classes. The keywords
publicandprivatecontrol who can use a class, variable, constructor, or method.privatemeans only code inside the same class can use it.publicmeans classes outside it can use it too.
-
3.3.A.2 In AP CSA, you always declare a class as
publicand use the keywordclass, as inpublic class Dog. -
3.3.A.3 In AP CSA, every constructor you write is declared
public. - 3.3.A.4 An instance variable is stored inside each object, so every object gets its own separate copy of it.
-
3.3.A.5 To keep a class's data hidden (encapsulation), only the class itself should touch its attributes. So the usual good practice is to mark instance variables
private, unless the problem says to do something different. -
3.3.A.6 Behaviors can be used inside or outside a class, depending on the method's access.
- A
publicmethod can be called from inside the class or from other classes. - A
privatemethod can only be called from inside its own class.
- A
Topic 3.4: Constructors
Practice this topic: Topics 3.3–3.4: Anatomy of a Class and Constructors
3.4.A — Write code that declares instance variables and sets their starting values inside a class's constructors.
- 3.4.A.1 An object's state is the set of its attributes and their current values at one moment. Instance variables store that state. This is a has-a relationship, because an object has its instance variables.
- 3.4.A.2 A constructor sets up a new object's starting state, and it should give a first value to every instance variable. Calling it reserves memory for the object and gives back a reference to it. Constructor parameters, if there are any, bring in the values for that setup.
- 3.4.A.3 If a constructor receives an object that can be changed (a mutable object), store a copy of it in the instance variable. Then your object does not point at the original, and its methods cannot accidentally change the original object's state.
- 3.4.A.4 If you write no constructor at all, Java supplies one for you that takes no parameters. It sets each instance variable to a default value that matches its type. This is called the default constructor.
-
3.4.A.5 Default values by type:
intstarts as0.doublestarts as0.0.booleanstarts asfalse.- Any reference type (like
String) starts asnull.
Topic 3.5: Methods: How to Write Them
Practice this topic: Topics 3.5–3.6: Writing Methods and Passing References
3.5.A — Write methods in a class that define what an object does with primitive values, and predict what happens when you call them.
-
3.5.A.1 A
voidmethod does not give back a value. You can spot one because the keywordvoidsits in its header right before the method name. -
3.5.A.2 A non-void method gives back exactly one value. Its header lists the type of that value (the return type) where
voidwould otherwise go. -
3.5.A.3 In a non-void method, Java works out the value of the expression after
return, which must fit the declared return type, and sends that value back. This is called return by value. -
3.5.A.4
returnsends control back to the spot where the method or constructor was called. Code placed right after areturnstatement never runs. Areturninside anifor a loop stops that statement and exits the whole method or constructor. - 3.5.A.5 An accessor method lets other classes read the value of an instance variable or class variable, and they get a copy of that value. An accessor is a non-void method, since it has to return something.
-
3.5.A.6 A mutator method (also called a modifier) changes the value of an instance variable or class variable. Mutators are often
voidmethods. - 3.5.A.7 A method with parameters takes in values through them when it is called, and uses those values to do its work.
-
3.5.A.8 When you pass a primitive value (like an
int) as an argument, the parameter gets its own copy of that value. Changing the parameter inside the method does not change the original argument.
Topic 3.6: Methods: Passing and Returning References of an Object
Practice this topic: Topics 3.5–3.6: Writing Methods and Passing References
3.6.A — Write methods in a class that take or return object references, and predict the result of calling them.
- 3.6.A.1 When you pass an object as an argument, the parameter gets a copy of the reference (the address of the object), not a copy of the object itself. If the object is mutable, the method can use that reference to change the original object. Avoid changing objects passed in unless the problem asks for it.
- 3.6.A.2 If a method returns an object, it hands back the reference to that same object. It does not make a new copy of the object first.
-
3.6.A.3 A method cannot see the
privatedata or methods of an object that came in through a parameter. The one exception is when the parameter has the same type as the class the method is written in.
Topic 3.7: Class Variables and Methods
Practice this topic: Topics 3.7–3.9: Class Variables, Scope, and this
3.7.A — Write class methods (static methods) that define behavior belonging to the class itself.
-
3.7.A.1 A class method (one marked
static) has no object of its own. So it cannot use instance variables or call instance methods, unless you pass it an object of the class as a parameter. - 3.7.A.2 A class method can read and change class variables, and it can call other class methods.
3.7.B — Write declarations of class variables (static variables) that belong to the class and are shared by all its objects.
-
3.7.B.1 A class variable belongs to the class itself, so all objects of that class share a single copy. You declare one by putting the keyword
staticbefore the variable's type. -
3.7.B.2 A
publicclass variable belongs to the class, not to any one object. So from outside the class you reach it with the class name, a dot, then the variable name, likeClassName.variableName. -
3.7.B.3 A variable declared
finalcannot be given a new value once it is set.
Topic 3.8: Scope and Access
Practice this topic: Topics 3.7–3.9: Class Variables, Scope, and this
3.8.A — Explain in which parts of a program each variable can be used, based on where it is declared.
-
3.8.A.1 A local variable is one declared inside a block of code, either in its header or its body. You can only use it inside that block. Constructors and methods are blocks, so their parameters are local variables too. Local variables cannot be marked
publicorprivate. - 3.8.A.2 If a local variable or parameter has the same name as an instance variable, then inside that constructor or method the name means the local variable, not the instance variable.
Topic 3.9: this Keyword
Practice this topic: Topics 3.7–3.9: Class Variables, Scope, and this
3.9.A — Write expressions that use this to refer to the current object, and predict what they produce.
-
3.9.A.1 Inside an instance method or a constructor,
thisis a special variable that refers to the current object, meaning the one that the method or constructor is working on right now. -
3.9.A.2 You can write
thisas an argument in a method call to hand the current object to another method. -
3.9.A.3 Class methods (
staticmethods) have nothis, because they are not running on behalf of any particular object.