UseAccount
and UseAccountFromOutside
classes can set an account's customerName
and get the account's existing customerName
:account.customerName = "Occam";
String nameBackup = account.customerName;
But neither the UseAccount
class nor the UseAccountFromOutside
class can tinker with an account's internalIdNumber
field.
What if you want a class like UseAccount
to be able to get an existing account's internalIdNumber
but not to change an account's internalIdNumber
? (In many situations, getting information is necessary, but changing existing information is dangerous.) You can do all this with a getter method.
package com.allyourcode.bank;
public class Account {
public String customerName;
private int internalIdNumber;
String address;
String phone;
public int socialSecurityNumber;
int accountType;
double balance;
public static int findById(int internalIdNumber) {
Account foundAccount = new Account();
// Code to find the account goes here.
return foundAccount.internalIdNumber;
}
<strong> public int getInternalIdNumber() {</strong>
<strong> return internalIdNumber;</strong>
<strong> }</strong>
}
With the Account
class, another class's code can call
int backupIdNumber = account.getInternalIdNumber();
The Account
class's internalIdNumber
field is still private, so another class's code has no way to assign a value to an account's internalIdNumber
field. If you want to enable other classes to change an account's private internalIdNumber
value, you can add a setter method to the code, like this:
public void setInternalIdNumber(int internalIdNumber) {
this.internalIdNumber = internalIdNumber;
}
Getter and setter methods aren't built-in features in Java — they’re simply ordinary Java methods. But this pattern (having a method whose purpose is to access an otherwise inaccessible field's value) is used so often that programmers use the terms getter and setter to describe it.
Getter and setter methods are accessor methods. Java programmers almost always follow the convention of starting an accessor method name with get
or set
and then capitalizing the name of the field being accessed. For example, the field internalIdNumber
has accessors named getInternalIdNumber
and setInternalIdNumber
. The field renderingValu
e has accessors named getRenderingValue
and setRenderingValue
.
<strong>package com.allyourcode.bank;</strong>
<strong>public</strong> class Account {
<strong>public</strong> String customerName;
<strong>private</strong> int internalIdNumber;
String address;
String phone;
<strong>public</strong> int socialSecurityNumber;
int accountType;
double balance;
<strong>public</strong> static int findById(int internalIdNumber) {
Account foundAccount = new Account();
// Code to find the account goes here.
return foundAccount.internalIdNumber;
}
}
You can have Android Studio create getters and setters for you. Here's how:
- Start with your code in the Android Studio editor.
- Click the mouse cursor anywhere inside the editor.
- On the Android Studio main menu, select Code → Generate → Getter and Setter.
The Select Fields to Generate Getters and Setters dialog box appears. Alternatively, you can generate only getters by selecting Code → Generate → Getter. And you can generate only setters by selecting Code → Generate → Setter. A dialog box lists the fields in the class that appears in Android Studio's editor.
- Select one or more fields in the dialog box's list of fields.
To create the code above, you select only the
internalIdNumber
field. Alternatively, you can generate only getters by selecting Code - Click OK.
Android Studio dismisses the dialog box and adds freshly brewed getter and setter methods to the editor's code.