π5.4: Accessors/Getters
Table of Contents
π This page is a condensed version of CSAwesome Topic 5.4
Accessor Methods (Getters)
Since the instance variables in a class are usually marked as private
to the class, if you want code outside the class to be able to access the value of an instance variable, you need to write what is formally called an accessor methods but which everyone actually just calls a getter.
π A getter is a public
method that takes no arguments and returns the value of the private
instance variable.
class ExampleTemplate
{
// Instance variable declaration
private typeOfVar varName;
// Accessor (getter) method template
public typeOfVar getVarName()
{
return varName;
}
}
Notice that the getterβs return type is the same as the type of the instance variable and all the body of the getter does is return the value of the variable using a
return
statement.
Hereβs an example of an accessor method called getName
for the Student
class which also demonstrates how to call getName
using a Student
object:
public class Student
{
//Instance variable name
private String name;
/** getName() example
* @return name */
public String getName()
{
return name;
}
public static void main(String[] args)
{
// To call a get method, use objectName.getVarName()
Student s = new Student();
System.out.println("Name: " + s.getName() );
}
}
Note that getters only return the value of the variable. In other words, the code that called the getter and which receives that value has no ability to change the objectβs instance variable - they just get a copy of the value.
However if the instance variable is a reference (object) type like String
orPerson
the value that is copied is the value of the reference. That means the caller receives a new copy of the reference that points to the same object as is stored in the instance variable.
In the next section, when we talk about mutation, youβll see how that means that the caller might be able to change the object even though it canβt change the reference.
Some common errors when writing and using getters are:
- Forgetting a return type like
int
before the method name. - Forgetting to use the
return
keyword to return a value at the end of the method. - Forgetting to do something with the value returned from a method, like assigning it to a variable or printing it out.
The toString
Method
π While not strictly speaking a getter, another important method that returns a value is the toString
method. This method is called automatically by Java in a number of situations when it needs to convert an object to a String
.
Most notably the methods System.out.print
and System.out.println
use an objectβs toString
method to convert a object argument into a String
to be printed.
Here is the Student
class again, but this time with a toString
method:
public class Student {
private String name;
private String email;
private int id;
public Student(String initName, String initEmail, int initId) {
name = initName;
email = initEmail;
id = initId;
}
// toString() method
public String toString() {
return id + ": " + name + ", " + email;
}
}
Note that when we call System.out.println(s1)
in the tester class below, it will automatically call the toString
method within the Student
class to get a String
representation of the Student
object:
public class TesterClass {
// main method for testing
public static void main(String[] args) {
Student s1 = new Student("Skyler", "skyler@sky.com", 123456);
System.out.println(s1);
}
}
The
toString
method will return aString
that is then printed out.
π» In-Class Activity: Class Pet
βοΈ Summary
-
An accessor method, or getter, allows other objects to obtain the value of instance variables or static variables.
- A non-void method returns a single value. Its header includes the return type in place of the keyword
void
.- A getter is a non-void method that returns the value of an instance variable.
- Its return type matches the type of the instance variable.
-
Methods βreturn by valueβ where a copy of the value is returned. When the value is a primitive type, the value is copied. When the value is a reference to an object, the reference is copied, not the object.
-
The
return
keyword is used to return the flow of control to the point immediately following where the method or constructor was called. -
The
toString
method is an overridden method that is included in classes to provide a description of a specific object. It generally includes what values are stored in the instance data of the object. -
If
System.out.print
orSystem.out.println
is passed an object, that objectβstoString
method is called, and the returnedString
is printed. - An objectβs
toString
method is also used to get theString
representation used when concatenating the object to aString
with the+
operator.
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.