5.4 Accessor Methods
Enduring Understanding
Programmers use code to represent a physical object or nonphysical concept, real or imagined, by defining a class based on the attributes and/or behaviors of the object or concept.
Learning Objective
Define behaviors of an object through non-void methods without parameters written in a class.
Essential Knowledge
An accessor method 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.
In non-void methods, a return expression compatible with the return type is evaluated, and a copy of that value is returned. This is referred to as “return by value.”
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.
Last updated
Was this helpful?