define behaviors of an object through non-void methods without parameters written in a class
define behaviors of an object through void methods with or without parameters written in a class
ACCESSOR METHODS
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 the class.
Accessor methods are also known as non-void methods because they return something.
In this lesson we will also see mutator methods, also called set methods or setters, that allow a way to change the values of the instance variables.
Mutator methods are also known as void methods because they are void of a return type (they don't return anything).
Java programmers write get methods for each instance variable that look like the following:
Notice that the get methods return the instance variable's value and it has a return type that is the same type as the variable that it is returning.
Here is an example of an accessor method called getName() for the Student class which also demonstrates how to call getName() using a Student object:
Some of the common errors with methods that return values 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 saving it into a variable or printing it out).
RETURN BY VALUE
There is a subtle difference between 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 variable.
However, object variables really hold a reference to the object 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 designed to do this 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.
TOSTRING()
Another common accessor method that returns a value is the toString() method which returns a String description of the instance variables of the object.
This method is called automatically to try to convert an object to a String when it is needed, for example, in a print statement.
Here is the Student class again, but this time with a toString() method.
Notice that when we call System.out.prinln(s1); it will automatically call the toString() method to cast the object into a String.
The toString() mehtod will return a String that is then printed out.
MUTATOR METHODS
Corresponding to each get method, programmers also provide a public set method to change the value of a private instance variable in a class.
These are called mutator methods (or setters or set or modifier methods.
Remember, these are void methods which mean that they do not return a value, but they do take a parameter, the new value for the instance variable.
Here is a template showing how to write a mutator method for an instance variable:
I added a mutator method called setName() to the Student class:
Now you can see that I call the setName(String) method on the s1 object to reassign the value of the name variable from its default value ("name") to "Joe":
So, keep in mind that there is a difference between mutator (set methods) and accessor (get methods) methods.
The following figure compares the two:
Mutator methods do not have to have a name with "set" in it, although most do.
They can be any methods that change the value of an instance variable or static variable in a class.
CREATING OBJECTS WITH USER INPUT
Let's kick this lesson up a notch, shall we?
I was thinking about it and wondered if I would be able to use the Scanner class to get input from the user in order to create new Student objects!
Sure enough, it is possible!Â
I added the following code to the Main class and made sure to add import java.util.Scanner; at the top.
Here is what I was able to come up with, and you can see that it works:
Unfortunately, there is some sort of issue with input being skipped in this program...
This issue was actually fixed by adding the code on line 28 in the image above...
SUMMARY
An accessor method allows other objects to obtain the value of instance variables or static variabes.
A non-void method returns a single value. Its header includes the return type in place of the keyword void.
Accessor methods that return primitive types use "return by value" where a copy of the value is returned.
When the return expression is a reference to an object, a copy of that reference is returned, not a copy of 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 or System.out.println is passed an object, that object's toString() method is called, and the returned String is printed.
A void method does not return a value. Its header contains the keyword void before the method name.
A mutator method is often a void method that changes the values of instance variables or static variables.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) RESEARCH the reason why the Scanner object might skip over the input for a String after the object has been created. Explain to Mr. C why adding input.nextLine() to your code fixes the issue.
3) You have been hired to create a software program for Mr. C's Animal Clinic! I would like to be able to keep track of my animal patients. The attributes I would like to track are Name, Age, Weight, Type (dog, cat, lizard, etc.), Breed. Your task is to:
> Create a class that keeps track of the attributes above for pet records at the animal clinic. Decide what instance variables are needed and their data types. Make sure to use int, double, and String data types and ensure that they are private.
> Create 2 constructors, one without parameters (the no-argument constructor which sets default values) and one with many parameters to initialize all of the instance variables.
> Create Accessor (get) methods for each of the instance variables.
> Add Mutator (set) methods for each of the instance variables.
> Create a toString() method that returns all of the information in a pet record.
> In the main method, create at least 3 Pet objects and call their constructors, accessor methods, and toString methods to test all of your methods.
> Make sure to use the proper commenting shown in lesson WC.3!
4) MC Exam Practice: This lesson has additional practice problems which can be found on the MC Exam Practice page with an answer key! Use ctrl + f to search for different objectives throughout the year. You could look at these questions now, or, save these practice questions for later and use them to review! The choice is yours.