Java doesn't have static local variables in functions (like C has); final
means something completely different to what you're doing.
The only way you can get that kind of static is to use an instance or class member, e.g.:
class Foo {
private Cursor theCursor;
private synchronized Cursor getAllContactsCached() {
if (this.theCursor == null) {
this.theCursor = this.getList();
}
return this.theCursor;
}
}
(That's the instance-specific way; you can also do this in a class-wide way, but I'm guessing that isn't appropriate for a Cursor
.)
Note that the entire method is synchronized. This is important if it's crucial that you only ever have a single instance of the cursor. If it's merely an optimization, and not crucial, you could live with the race condition and not synchronize, in which case you could end up with two different cursors returned by the function. (You might be tempted to use the double-checked locking idiom, but it doesn't work with Java unless you use a volatile
variable, and it ends up just being better to go ahead and synchronize.)