Access Methods
-
An access method is a method that provides access to the values stored
as private istance variables in an object.
- Do you remember how we accessed the x and y coordinates of a
Point? We called the getX() or getY() method that returned the value.
- The typical
name for an access method is to use the word get
followed by the name of the instance value as the name of the method. This
is why access methods are also called get-methods or getters.
- What does a get-method do
and what does it return? It provides access to a value in an object by
returning the private instance variable. Getters do not take any arguments.
- This makes determining the get-methods
return type very simple - it matches the data type of the instance variable.
- Getters are public - this is so they can always be accessed
from outside of our class, for instance from a main().
So, how would we call these methods???
- For now we will provide insructions on how to code any helper classes, but once you are given the instance variables, you should know how to write the access methods (getters).