Java Programming for Android Developers For Dummies
Book image
Explore Book Buy On Amazon
When programming your Android app with Java, you could use getters and setters. In this code, the 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 renderingValue has accessors named getRenderingValue and setRenderingValue.

Before you begin, enter this code in the editor:

<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:

  1. Start with your code in the Android Studio editor.
  2. Click the mouse cursor anywhere inside the editor.
  3. 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.

  4. 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

  5. Click OK.

    Android Studio dismisses the dialog box and adds freshly brewed getter and setter methods to the editor's code.

java-programming-for-android-developers-2e-getters-and-setters
Select Fields to Generate Getters and Setters.

About This Article

This article is from the book:

About the book author:

Barry Burd, PhD, is a professor in the Department of Mathematics and Computer Science at Drew University in Madison, New Jersey. He has lectured at conferences in the United States, Europe, Australia, and Asia. He hosts podcasts and videos about software and other technology topics. He is the author of many articles and books, including Java For Dummies.

This article can be found in the category: