| Learning Objective | Essential Knowledge |
|---|---|
3.1.A |
|
Class Creation
AP Computer Science A · Topic 3
7:26
Class Creation
Here is a student, stored as three loose variables: a name, a year, and a grade. That works. Now add a thousand students. You need three thousand variables…
English narration · English + 中文 subtitles burned in
3.1
Abstraction and Program Design
Syllabus
Source: College Board AP Course and Exam Description

Abstraction 抽象 means hiding detail behind a simple interface 接口 – you use a String without knowing how it stores characters. Good design breaks a problem into classes, each responsible for one idea. This topic is about writing your own classes.

| English |
|---|
| Abstraction/əbˈstrækʃn/ |
| interface/ˈɪntəfeɪs/ |
3.2
The Impact of Program Design
Syllabus
| Learning Objective | Essential Knowledge |
|---|---|
3.2.A |
|
Source: College Board AP Course and Exam Description
Design choices affect whether code is correct, readable 可读的, and reusable 可复用的. Encapsulation 封装 – keeping data private 私有的 and exposing it only through methods 方法 – protects an object's state 状态 from misuse and lets you change the inside without breaking users of the class. Thoughtful naming, single-purpose methods, and testing 测试 reduce bugs 缺陷.
Design also carries responsibility beyond the code. System reliability 系统可靠性 - a program performing its tasks as expected, without failure - is something programmers should maximise through careful design and testing. Programs have real impacts on society, the economy, and culture that can be both beneficial 有益的 and harmful 有害的. And creating programs raises legal 法律 and intellectual-property 知识产权 concerns: programmers often reuse code published as open source 开源 and free to use, but must respect its licence 许可证 and give credit 注明出处 rather than copy others' work as their own.
| English |
|---|
| readable |
| reusable/riːˈjuːzəbl/ |
| Encapsulation/ɪnˌkæpsjʊˈleɪʃn/ |
| private/ˈpraɪvət/ |
| methods/ˈmeθədz/ |
| state/steɪt/ |
| testing/ˈtestɪŋ/ |
| bugs |
| System reliability/ˈsɪstəm rɪˌlaɪəˈbɪlɪti/ |
| beneficial/ˌbenɪˈfɪʃl/ |
| harmful/ˈhɑːmfl/ |
| legal/ˈliːɡl/ |
| intellectual-property |
| open source/ˈəʊpən sɔːs/ |
| licence/ˈlaɪsəns/ |
| give credit |
3.3
The Anatomy of a Class
Syllabus
| Learning Objective | Essential Knowledge |
|---|---|
3.3.A |
|
Source: College Board AP Course and Exam Description
A class has three parts: instance variables 实例变量 (fields 字段 – the object's data), constructors (build objects), and methods (behavior 行为). Fields are usually private; methods are usually public:

public class Student {
private String name; // instance variable
private int score;
public Student(String n, int s) { // constructor
name = n;
score = s;
}
public int getScore() { return score; } // accessor
}

Common mistake. Repeating the type inside the constructor declares a new local variable instead of setting the field:
public Student(String n, int s) {
String name = n; // local variable: the field name stays null
score = s; // correct: sets the field
}
The code compiles, but every Student built this way has name equal to null.
See an object's fields as boxes
A class groups related data (its fields) and methods. Each object gets its own set of field boxes; assigning to one changes that object only.
| English |
|---|
| instance variables/ˈɪnstəns ˈveərɪəblz/ |
| fields |
| behavior/bɪˈheɪvjə/ |
3.4
Constructors
Syllabus
| Learning Objective | Essential Knowledge |
|---|---|
3.4.A |
|
Source: College Board AP Course and Exam Description
A constructor 构造函数 has the same name as the class and no return type 返回类型. It runs when you write new, and its job is to initialize 初始化 the fields. A class can have several constructors with different parameter lists 形参列表 (overloading 重载); a no-argument 无参 constructor accepts no arguments and can choose suitable defaults 默认值. Before constructor assignments, instance fields have Java’s type-dependent initial values, such as 0 for int and null for references.
Worked example. A Point class with two constructors:
public class Point {
private int x, y;
public Point() { // no-argument: the origin
x = 0;
y = 0;
}
public Point(int a, int b) { // two int parameters
x = a;
y = b;
}
}
| Call | Constructor used | Fields |
|---|---|---|
new Point() |
no-argument | x = 0, y = 0 |
new Point(3, 4) |
(int, int) |
x = 3, y = 4 |
new Point(3) |
none matches | does not compile |
A field that a constructor does not set keeps its default value: 0 for int, 0.0 for double, false for boolean and null for an object. Writing public void Point() makes a method, not a constructor: a constructor has no return type, not even void.
| English |
|---|
| constructor/kənˈstrʌktə/ |
| return type |
| initialize |
| parameter lists |
| overloading/ˌəʊvəˈləʊdɪŋ/ |
| no-argument |
| defaults |
3.5
Methods: How to Write Them
Syllabus
| Learning Objective | Essential Knowledge |
|---|---|
3.5.A |
|
Source: College Board AP Course and Exam Description
A method has a signature 方法签名, a return type, and a body 方法体. An accessor (getter) 访问器 returns information without changing the object; a mutator (setter) 修改器 changes a field. A method returning a value must have a return of the right type on every path; a void method returns nothing.
public void setScore(int s) { score = s; } // mutator
public String toString() { return name + ": " + score; }
Worked example. Write a mutator addBonus(int b) that adds b to the score but never lets it go above 100, and returns the new score.
public int addBonus(int b) {
score += b;
if (score > 100) {
score = 100;
}
return score;
}
- The return type is
int, because the method gives back the new score. - The parameter
bis used; the fieldscoreis changed, so this is a mutator. - For a score of 90,
addBonus(15)sets the score to 100 and returns100.
A common mistake is a return inside the if only: if (score > 100) { return 100; } with nothing after it does not compile, because the other path returns nothing. Another is printing the answer instead of returning it: a method asked to return a value must not just System.out.println it.
Follow a method call and its return
Calling a method pushes a frame with its parameters; when it hits return, the frame pops and the value goes back to the caller.
| English |
|---|
| signature/ˈsɪɡnɪtʃə/ |
| body/ˈbɒdi/ |
| accessor (getter)/əkˈsesə/ |
| mutator (setter)/mjuːˈteɪtə/ |
3.6
Passing and Returning References of an Object
Syllabus
| Learning Objective | Essential Knowledge |
|---|---|
3.6.A |
|
Source: College Board AP Course and Exam Description
= copies the reference, not the objectWhen you pass an object to a method, Java copies the reference 引用, so the method acts on the same object – changes to its fields are visible to the caller 调用者. (Primitives 基本类型 are copied by value 按值复制, so changes to them are not.) A method can also return a reference to an object. Because a String is immutable 不可变, passing one is safe; passing a mutable 可变 object lets a method change it.

Exam skill: know that mutating an object's fields inside a method affects the original, but reassigning the parameter (param = new...) does not affect the caller.
Worked example. Suppose s is a Student with score 50, and we call tweak(s):
public static void tweak(Student a) {
a.setScore(100); // (1) mutates the shared object
a = new Student("Z", 0); // (2) repoints the local copy only
a.setScore(5); // (3) changes only the new local object
}
Line (1) changes the object s points to, so the caller now sees 100. Line (2) makes the method's own copy of the reference point at a fresh object – the caller's s is untouched – and line (3) affects only that new object. After the call, s.getScore() is 100: the mutation stuck, the reassignment did not.
| English |
|---|
| reference/ˈrefrəns/ |
| caller |
| Primitives/ˈprɪmɪtɪvz/ |
| copied by value |
| immutable/ɪˈmjuːtəbl/ |
| mutable |
3.7
Class Variables and Class Methods
Syllabus
| Learning Objective | Essential Knowledge |
|---|---|
3.7.A |
|
3.7.B |
|
Source: College Board AP Course and Exam Description
A static (class) variable 类变量, marked static, is shared by all objects of the class – one copy total (e.g. a counter of how many objects exist). A static method 静态方法 belongs to the class and cannot use instance fields directly. Access them by class name: Student.getCount().
Worked example. Give each new Ticket a number, counting up from 1:
public class Ticket {
private static int count = 0; // one copy, shared
private int number; // one per object
public Ticket() {
count++;
number = count;
}
public int getNumber() { return number; }
public static int getCount() { return count; }
}
After Ticket a = new Ticket(); Ticket b = new Ticket(); Ticket c = new Ticket();, a.getNumber() is 1, c.getNumber() is 3 and Ticket.getCount() is 3. If count were not static, each object would have its own count, every ticket would be number 1, and getCount could not be static.
| English |
|---|
| static (class) variable/ˈstætɪk ˈveərɪəbl/ |
| static method/ˈstætɪk ˈmeθəd/ |
3.8
Scope and Access
Syllabus
| Learning Objective | Essential Knowledge |
|---|---|
3.8.A |
|
Source: College Board AP Course and Exam Description
Scope 作用域 is where a name is visible. A local variable 局部变量 declared in a method exists only inside it; a parameter 形参 exists only in its method; an instance variable is visible throughout the object. Access modifiers 访问修饰符 control visibility 可见性 across classes: private (this class only) versus public (anywhere). Local variables shadow 遮蔽 fields of the same name – a source of bugs.
Worked example. Find the bug:
public void setScore(int score) {
score = score;
}
The parameter score shadows the field, so both sides mean the parameter: the method copies the parameter onto itself and the field never changes. The fix is this.score = score; (see the next section) or a different parameter name such as newScore.
| Name | Declared in | Visible in |
|---|---|---|
| instance variable | the class, outside methods | every method of the class |
| parameter | the method header | that method only |
| local variable | a method or block | from its line to the end of its block |
loop variable in for (int i …) |
the for header |
that loop only |

| English |
|---|
| Scope/skəʊp/ |
| local variable/ˈləʊkl ˈveərɪəbl/ |
| parameter/pəˈræmɪtə/ |
| Access modifiers |
| visibility |
| shadow/ˈʃædəʊ/ |
3.9
The this Keyword
Syllabus
| Learning Objective | Essential Knowledge |
|---|---|
3.9.A |
|
Source: College Board AP Course and Exam Description
this is a reference to the current object 当前对象. Use it to tell a field apart from a parameter with the same name, or to call another method of the same object:
public Student(String name, int score) {
this.name = name; // this.name is the field; name is the parameter
this.score = score;
}
Exam skill: when a constructor or setter's parameter has the same name as a field, you must write this.field = param – without this, the assignment does nothing useful.
| English |
|---|
| current object |
3.9
Exam tips
- Design with methods and classes: encapsulate data as
privatefields and expose behaviour through public methods. - Know the difference between an object and its class, and that objects are passed by value — the parameter gets a copy of the reference, so a method can change the object's state, but reassigning the parameter does not affect the caller (Java has no pass-by-reference).
- Traverse arrays and
ArrayLists safely — size islengthvs.size(), and removing during a loop shifts indices. - Trace a recursive method to determine its result: find the base case 基本情况 first, then follow each recursive call 递归调用 to its return value (writing recursive code is outside the exam's scope).
- Recognise inheritance 继承 vocabulary — superclass 父类, subclass 子类, method overriding 方法重写, and that every Java class other than
Objectis a direct or indirect subclass ofObject(designing and implementing inheritance is outside the exam's scope).
| English |
|---|
| base case/beɪs keɪs/ |
| recursive call/rɪˈkɜːsɪv kɔːl/ |
| inheritance/ɪnˈherɪtəns/ |
| superclass/ˈsuːpəklæs/ |
| subclass/ˈsʌbklæs/ |
| method overriding/ˈmeθəd ˌəʊvəˈraɪdɪŋ/ |
Interactive lessons on this topic
Work through it step by step, with instant-check exercises.