AP® Computer Science A glossary
Accessor method
Also called: getter
Course: AP® Computer Science A Shows up in: Unit 3: Class Creation
A non-void method that lets objects of other classes obtain a copy of the value of an instance variable or class variable.
Appears in the course and exam description (CED): 3.5.A.5
Accessors keep instance variables private while still letting other classes read them. For a primitive variable, the caller receives a copy of the value; changing that copy does not change the object.
public class Bottle
{
private int amount;
public Bottle(int a)
{
amount = a;
}
public int getAmount() // accessor
{
return amount;
}
} Learn it properly, with a checkpoint 3.5 Methods: How to Write Them