1/12
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
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. (We discussed using getters in section 2.5.)
You don’t need to write a getter for every instance variable in a class but if you want code outside the class to be able to get the value of one of your instance variables, you’ll need to write a getter that looks like the following.
classExampleTemplate{
// Instance variable declarationprivate typeOfVar varName;
// Accessor (getter) method templatepublic typeOfVar getVarName() {
return varName;
}
}
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 type like String or Person 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.
Note
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.
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 it to convert a object argument into a String to be printed and when objects are added to Strings with + and += their String representation comes from calling their toString method.
A 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 or System.out.println is passed an object, that object’s toString method is called, and the returned String is printed.
An object’s toString method is also used to get the String representation used when concatenating the object to a String with the + operator.