Since the instance variables in a class are usually marked as private to the class, programmers provide public methods that allow safe access to the instance variable values in a class. Accessor methods, also called get methods or getters, allow a way to get the value of each instance variable from outside of the class. Next we’ll review mutator methods, which are also called set methods or setters, that allow a way to change the values of the instance variables.
Java programmers write get methods for each instance variable that look like the following. Notice that the get method returns the instance variable’s value and it has a return type that is the same type as the variable that it is returning.
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() );
}
Note6.4.1.
Some common errors with methods that return values are:
Try the following code. Note that this active code window has 2 classes! The main method is in a separate Tester or Driver class. It does not have access to the private instance variables in the other Student class. Note that when you use multiple classes in an IDE, you usually put them in separate files, and you give the files the same name as the public class in them. In active code and IDEs, you can put 2 classes in 1 file, as demonstrated here, but only 1 of them can be public and have a main method in it. You can also view the fixed code in the Java visualizer.
There is a subtle difference in methods that return primitive types versus reference/object types. If the method is returning a primitive type like int, it returns a copy of the value. This is called return by value. This means the original value is not changed and it is a safe way to access the instance variables.
However, object variables really hold a reference to the object in memory. This is not the actual value, but its address in memory. So, if the method is returning an object like String, Java returns a copy of the object reference, not the value itself. Java was especially designed this way because objects tend to be large and we want to avoid copying large objects, so we just pass around references to the objects (their addresses in memory). So, when we call getName(), we actually get back a reference to the String for the name in memory.
Here is the Student class again, but this time with a toString() method. Note that when we call System.out.println(s1); it will automatically call the toString() method to cast the object into a String. The toString() method will return a String that is then printed out. Watch how the control moves to the toString() method and then comes back to main in the Java visualizer or by using the Code Lens button.
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.
Consider the following Party class. The getNumOfPeople method is intended to allow methods in other classes to access a Party object’s numOfPeople instance variable value; however, it does not work as intended. Which of the following best explains why the getNumOfPeople method does NOT work as intended?
Try the following code. Note that it has a bug! It tries to access the private instance variable email from outside the class Student. Change the main method in Tester class so that it uses the appropriate public accessor method (get method) to access the email value instead.