AP Computer Science A, AP exams, 10–18%
Unit 3: Class Creation
Unit 3 moves from using classes to writing them. Instance variables, constructors, accessor and mutator methods, static members, scope, and the this reference are the content, along with documenting behavior through comments and preconditions. One of the four free-response questions typically asks students to design and implement a class from a specification.
Unit 3: Class Creation at a glance
- Position
- 3 of 4 units
- Published weighting
- 10–18% in the official framework
- Topics covered
- 6 topics in this guide
What is covered
Topics inside this unit.
These topics describe the scope students most often review for this part of AP Computer Science A. Treat them as a study checklist rather than an exhaustive list of what a single exam form can sample.
- Anatomy of a class
- Instance variables, methods, and the access modifiers that control visibility.
- Constructors
- Initializing instance variables, writing multiple constructors, and the role of parameters.
- Accessor and mutator methods
- Providing controlled access to private state, and the conventions the exam expects.
- Writing methods
- Implementing behavior from a specification, including return types and parameter handling.
- Static variables and methods
- Class-level state and behavior, and when static is appropriate rather than instance-level.
- Scope, access, and the this reference
- Local versus instance scope, why encapsulation matters, and using this to disambiguate.
Study tips
How to practice Class Creation.
Section-specific guidance that follows from how this material is usually tested.
- Write a complete class from a written specification at least once a week; it is a standard free-response task.
- Keep instance variables private and provide accessors, since the rubrics expect proper encapsulation.
- Practice reading a specification and listing the required instance variables and method signatures before coding.
Where this fits
Unit 3: Class Creation is 3 of 4 units in the AP Computer Science A guide, and it carries a published weighting of 10–18% in the official framework. Use the full guide to balance it against everything else you have to review.
Sample Class Creation questions
Practice questions, not yet expert-reviewedOriginal practice questions written to the published framework for this section. No expert has reviewed them, and they are not taken from any official exam. Try each one before you open the answer.
- Question 1Foundational
In Java, what is the name of the special method that runs when an object is created with new and initializes its instance variables?
- AThe constructor
- BThe main method
- CAn accessor method
- DA mutator method
Show answer
Answer: A. The constructor
A constructor shares the class name, has no return type, and runs exactly once when new creates the object. Accessors and mutators are ordinary methods called later on an existing object, and main is only the program's entry point.
Key insight: A constructor has the class name and no return type, which is what makes it run on object creation.
- Question 2Foundational
Which access modifier should instance variables use so that code outside the class cannot read or change them directly?
- Aprivate
- Bpublic
- Cstatic
- Dfinal
Show answer
Answer: A. private
Declaring instance variables private is the encapsulation convention the exam rubrics expect, forcing outside code to go through accessor and mutator methods. Static controls whether a variable belongs to the class rather than to each object, and final prevents reassignment without hiding the value.
Key insight: Encapsulation means private state with public methods that control how it is read and changed.
- Question 3Intermediate
A Java class Student has a private instance variable String name. Which method header declares a standard accessor for it?
- Apublic String getName()
- Bpublic void getName(String name)
- Cprivate String getName()
- Dpublic void setName(String name)
Show answer
Answer: A. public String getName()
An accessor is public so other classes can call it, takes no parameters, and returns the type of the variable it exposes. A void return type cannot hand back the value, and the last choice is a mutator, which changes the variable instead of reporting it.
Key insight: Accessors return a value and take nothing; mutators take a value and return nothing.
- Question 4Intermediate
A Java class needs one variable that counts how many objects of the class have been created and is shared by every object. How should that variable be declared?
- AAs a static variable
- BAs a private instance variable
- CAs a local variable inside the constructor
- DAs a parameter of the constructor
Show answer
Answer: A. As a static variable
A static variable belongs to the class, so a single copy is shared and can be incremented in the constructor to count objects. An instance variable would give every object its own separate counter, and a local variable would vanish when the constructor finished.
Key insight: Static means one copy per class; instance means one copy per object.
- Question 5Advanced
In a Java class Dog with a private instance variable String name, the constructor public Dog(String name) { name = name; } leaves that instance variable null after a Dog is constructed. What explains this and corrects it?
- AThe parameter shadows the field, so the constructor must use this.name = name
- BString objects cannot be assigned inside a constructor
- CThe constructor must declare a return type before it can assign a field
- DThe instance variable must be declared static to be assigned
Show answer
Answer: A. The parameter shadows the field, so the constructor must use this.name = name
Inside the constructor the identifier name refers to the parameter, so name = name assigns the parameter to itself and leaves the field untouched. Writing this.name = name names the instance variable explicitly, or the parameter could simply be renamed.
Key insight: When a parameter shares a field's name, only this makes the field reachable.
Continue through AP Computer Science A
Practice Class Creation deliberately.
Create your Arcade Academy workspace to turn this topic list into spaced-repetition reviews, linked notes, and progress you can act on.