Android App Development: Java Static Fields and Methods

By Barry Burd

Java is an object-oriented programming language. So, as an Android app developer, your primary goal is to describe objects. Your closely related goal is to describe objects’ close cousins — namely, classes. A class is the idea behind a certain kind of thing. An object is a concrete instance of a class.

Here, a small portion of the source code of Android’s Toast class is reproduced.

public class Toast {
public static final int LENGTH_LONG = 1;
public static Toast makeText(Context context,
CharSequence text,
int duration) {
Toast result = new Toast(context);
LayoutInflater inflate = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate
(com.android.internal.
R.layout.transient_notification, null);
TextView tv = (TextView)v.findViewById
(com.android.internal.R.id.message);
tv.setText(text);
result.mNextView = v;
result.mDuration = duration;
return result;
}
public void show() {
if (mNextView == null) {
throw new RuntimeException
(“setView must have been called”);
}
INotificationManager service = getService();
String pkg = mContext.getPackageName();
TN tn = mTN;
try {
service.enqueueToast(pkg, tn, mDuration);
} catch (RemoteException e) {
// Empty
}
}
}

According to this code, the Toast class has a static field named LENGTH_LONG and a static method named makeText. Anything that’s declared to be static belongs to the whole class, not to any particular instance of the class. When you create the static field, LENGTH_LONG, you create only one copy of the field. This copy stays with the entire Toast class. No matter how many instances of the Toast class you create — one, nine, or none — you have just one LENGTH_LONG field.

Contrast this with this situation. Here, the Account class has fields name, address, and balance. The fields aren’t static, so every instance of the Account class has its own name, its own address, and its own balance. One instance has name Barry Burd and balance 24.02, and another instance has name John Q. Public with balance –471.03. To refer to Burd’s balance, you may write something like myAccount.balance, as in the following code:

Account myAccount = new Account();
myAccount.name = “Burd”;
myAccount.address = “222 Cyberspace Lane”;
myAccount.balance = 24.02;

To refer to a non-static member of a class, you write the name of an object (such as myAccount), followed by a dot, and then the name of the member (such as balance).

But the Toast class’s LENGTH_LONG field is static. When you create a Toast instance, you don’t create a new LENGTH_LONG field. The Toast class has one LENGTH_LONG field, and that’s that. Accordingly, you refer to LENGTH_LONG by prefacing the field name with the Toast class name, followed by a dot:

Toast.LENGTH_LONG

In fact, a typical use of Toast in an Android app refers to the static field LENGTH_LONG and the static method makeText:

Toast.makeText
(getApplication(), “Whoa!”, Toast.LENGTH_LONG).show();

A call to the Toast class’s makeText method returns an actual object — an instance of the Toast class. (You can verify this by referring to the first line of the makeText method above.) So in an Android app, an expression such as

Toast.makeText
(getApplication(), “Whoa!”, Toast.LENGTH_LONG)

stands for an object. And each object created from the Toast class has its own non-static show method. That’s why you normally follow a Toast.makeText call with .show().

Here’s one final word about the code you find here: In addition to being static, the LENGTH_LONG field is also final. A final field is one whose value cannot be changed. In other words, when you declare LENGTH_LONG, you can initialize its value to 1. But elsewhere in the code, you can’t write LENGTH_LONG = 2. (For that matter, you can’t even write LENGTH_LONG = 1 elsewhere in the code.)

Many programming languages use the word constant (or the abbreviation const) to refer to a variable whose value cannot be changed.