📖 Crammy · All study guides
AP Computer Science A · Unit 5

Writing Classes: every key term you need (+ practice quiz)

55 flashcard terms for AP Computer Science A Unit 5, written to match the course framework. Read them here, drill them as flashcards, or take the 43-question quiz. Free, no account needed.

Study this unit free →

More AP Computer Science A guides

Classes & Objects
Class: blueprint defining fields (data) and methods (functions). Object: instance of class with specific values.
Encapsulation
Hide internal details (private fields), provide controlled access (public methods). Protects data, simplifies interface.
Access Modifiers
public: accessible everywhere. private: accessible only within class. default (no modifier): accessible in package. protected: subclasses can access.
Instance Variables (Fields)
Belong to object; each instance has own copy. Declared in class; initialized in constructor or at declaration.
Class Variables (static)
Shared by all instances. Declared static. Access via ClassName.variable. Example: counter tracking total objects created.
Instance Methods
Act on object; access this (current object). Example: getName() returns object's name.
Class Methods (static)
Act on class, not instance. No access to 'this' or instance variables. Example: Math.sqrt(), Integer.parseInt().
Constructors
Initialize object; same name as class. Can have parameters. Default no-arg constructor provided if none written.
this Keyword
Refers to current object. Used to distinguish instance variables from parameters: this.name = name;
Getters & Setters
Getter: public method returning field (getName()). Setter: public method modifying field (setName(String)). Control access/validation.
Method Overloading
Multiple methods same name, different parameter types/number. Example: int add(int, int) and double add(double, double).
Unit 5 Summary
Classes combine data (fields) and behavior (methods). Encapsulation controls access. Constructors initialize; getters/setters manage data.
Constructor Chaining with this(...)
One constructor may call another in the same class using this(args) as its first statement, so default values are defined in one place.
Default Constructor
If a class declares no constructor, Java supplies a no-argument constructor that leaves fields at defaults (0, 0.0, false, null). Once you write any constructor, that default disappears.
Instance Variable Defaults
Numeric fields default to 0, booleans to false, references to null. Local variables get no defaults and must be assigned before use.
Shadowing
A parameter or local variable with the same name as a field hides the field. this.name = name; distinguishes the field (left) from the parameter (right).
Accessor vs Mutator
Accessors (getters) return field values without changing state; mutators (setters) change state and are usually void. Encapsulation means fields are private and touched only through these.
Returning Copies for Mutable Fields
A getter that returns a reference to a private mutable object (like an ArrayList) lets outsiders modify internal state. Return a copy to preserve encapsulation.
Drill these as interactive flashcards →
Static Variable Sharing
One copy exists for the whole class, shared by all instances. A static counter incremented in the constructor counts how many objects have been created.
Static Method Restrictions
Static methods cannot use this or access instance variables directly because there may be no object. They may access static fields and other static methods.
Pass-by-value of References
Java passes a copy of the reference. The method can mutate the object the reference points to, but reassigning the parameter to a new object does not affect the caller's variable.
Primitive Parameters are Copies
Changing an int parameter inside a method never changes the caller's variable. To 'return' a changed value, use the return statement.
Method Precondition
A condition the caller must guarantee before calling (documented with @param or 'Precondition:'). The method need not check it; the AP exam says you may assume preconditions hold.
Method Postcondition
What is guaranteed to be true after the method returns, given the precondition held. FRQ solutions are graded against postconditions.
toString Override
public String toString() returns a String description; System.out.println(obj) and string concatenation call it automatically. Signature must be exact: public, returns String, no parameters.
Overloading Resolution
With print(int) and print(double), calling print(3) picks the int version and print(3.0) the double version. print('a') widens char to int.
Immutable Class Design
All fields private and final, no mutators, methods return new objects. Immutable objects are safe to share; String is the standard example.
Data Encapsulation Rationale
Private fields let a class enforce invariants (balance never negative) and change internal representation without breaking callers.
Static Constants
public static final double PI = 3.14159; is shared, unchangeable, and accessed via ClassName.PI. Combining static and final is the idiom for class-wide constants.
Method Decomposition
Break a large task into helper methods that each do one thing; helpers may be private if used only within the class. Improves readability and testing.
Constructor Does Not Return
Constructors have no return type, not even void; writing void before the class name turns it into an ordinary method that never runs on new.
Ethical and Social Implications
The CED asks students to consider how programs affect people, e.g. storing personal data privately, and to give credit for code sources (comments citing origins).
Object State vs Behavior
State is the collection of instance variable values at a moment; behavior is what methods do. Two objects with equal state are still distinct objects.
Class Invariant
A condition every constructor must establish and every public method must preserve, such as a BankAccount balance never being negative.
Constructor Validates Before Assigning
Checking arguments inside the constructor prevents an object from ever existing in an illegal state, which is stronger than checking later in a mutator.
this(...) Delegation Rules
A constructor may call another constructor of the same class with this(...), but only as its very first statement and never together with super(...).
Test yourself on this unit →
Defensive Copy on Input
Storing a copy of an array parameter rather than the reference stops the caller from mutating the object's internals afterward.
Defensive Copy on Output
Returning a copy of a mutable field keeps encapsulation intact; returning the field itself hands out a writable alias to private state.
Static Counter Idiom
A private static int shared by all instances and incremented in every constructor counts how many objects have been created.
Static Cannot Touch Instance State
A static method has no this, so it cannot read instance variables directly and must receive an object as a parameter instead.
Initialization Order in a Class
Instance variables receive default values, then explicit initializers run, then the constructor body executes.
Shadowing and this
When a parameter has the same name as a field, the unqualified name refers to the parameter and this.field is required to reach the instance variable.
Overloading Resolution Is Static
The compiler chooses among overloads using the declared types of the arguments, so a variable of type Object selects the Object version even if it holds a String.
Immutable Class Recipe
Make fields private and final, assign them only in the constructor, provide no mutators, and return copies of any mutable field.
toString Contract
Return a concise, human-readable description; println and concatenation call it automatically, so a missing override prints a hash code.
Precondition vs Validation
A documented precondition shifts responsibility to the caller and is not checked; validation actively rejects bad input inside the method.
Postcondition Describes the Result
It states what is true after the method returns, including which fields changed, and is the basis for reasoning about a method without reading its body.
Accessor Returning Computed Value
An accessor need not map to a stored field; area() can compute width * height so the object never stores redundant, drift-prone data.
Helper Method Privacy
A method used only inside the class should be private, which keeps the public interface small and lets the implementation change freely.
Static Constants for Shared Limits
public static final int MAX_SIZE = 100; gives one authoritative value that every instance and every caller can reference by class name.
Parameter Reassignment Is Local
Assigning a new value to a parameter changes only the method's copy; the caller's variable is unaffected even for object references.
Cohesion of a Class
A well-designed class has one clear responsibility; fields and methods serving unrelated purposes signal that the class should be split.
Encapsulation Enables Change
Because callers depend only on the public methods, the internal representation can be replaced without breaking any client code.
Object Equality Design
Two logically equal objects need an equals override; without one, the inherited version compares references and duplicates are never detected.
Ask the AI tutor about this unit →
Responsible Class Design
Storing only the data a program actually needs limits the harm of a breach and is the practical form of data-privacy responsibility.
Turn these into flashcards & quizzes →