AP Computer Science A / Chapter 3
Class Creation
Cheat sheet
Responsibilities
- A class groups related attributes and behaviors. An individual object owns its own state; a collection owner coordinates searches across its members.
- Data abstraction names meaningful properties without exposing their storage representation.
Named operations
- Procedural abstraction lets a caller use an operation through its contract. Coherent helper methods decompose larger behaviors.
- Parameters generalize one procedure across inputs. Internal changes preserve callers when accepted inputs and promised behavior remain stable.
Reliability and effects
- Test the stated requirement under ordinary, boundary and failure conditions. Running without a crash does not establish correct results.
- Trace intended and unintended impacts to design choices and affected people. A mitigation must address the mechanism producing the harm.
- Public source access does not establish unrestricted reuse permission. Read applicable terms and retain required attribution.
Encapsulation
- Public operations form a client interface. Private fields hide representation from unrelated classes; private helpers remain internal.
- Separate constructed objects have separate instance fields. A method’s receiver selects the instance state it uses.
Initial state
- A constructor has the class name and no return type. Declare every constructor needed by the intended interface.
- Java supplies a no-argument default constructor when no constructor is declared. Declaring a parameterized constructor removes that supplied alternative.
- Untouched instance fields start at numeric zero, false or null. Local variables need assignment before reading.
Retaining inputs
- Assigning a mutable input reference to a field shares the object. A defensive copy constructs independent retained state from the input’s values.
- Copy depth must match the promised independence; a new outer object may still retain shared inner references.
Methods and returns
- A mutator changes retained state. Assigning a primitive parameter does not change the caller’s variable.
- A void method returns no value; a bare return may finish it early. A non-void method needs a compatible result on every normally completing path.
- A return exits the entire current method, including a surrounding loop or conditional. A primitive accessor result is a copied value.
Reference parameters and results
- A reference parameter holds a local copy of a reference. Mutation can reach its shared object, while parameter reassignment does not replace the caller’s reference.
- Returning an existing mutable reference exposes shared identity. Constructing an independent result separates later mutation.
- Inside the declaring class, a method may read another instance’s private fields. Unrelated top-level classes remain outside that boundary.
Class state
- A static field has one shared value across instances. New construction does not reset it unless code explicitly does so.
- A static method has no implicit receiver; explicit object parameters can supply instance state. Static fields and methods can be used directly in class context.
- Access public class fields through the class name. An initialized final variable cannot be reassigned; a final reference does not itself make its object immutable.
Scope and shadowing
- A local name is available from its declaration through its enclosing block, including nested blocks. A narrower block does not make its locals visible outside.
- Parameters are local to their method or constructor. A same-named parameter or local hides the field for unqualified references.
- Local declarations and parameters do not take public or private access modifiers.
The current receiver
- this identifies the current object in an instance context. this.field distinguishes a field from a same-named local or parameter.
- Passing this supplies the existing receiver as an argument. Constructing a new object supplies a different identity.
- Static methods have no this reference; use an explicit object when instance state is required.