I am new to Android. I went through the code of Android ContentProvider
and I am a little bit confused with the UriMatcher
because the statements within static will execute first. Here in the code inside UriMatcher
there is a static block without having idea about PROVIDER_NAME
. How can it use it because before PROVIDER_NAME
is initialized the static block should run.
public class CustomContentProvider extends ContentProvider {
public static final String PROVIDER_NAME =
"net.learn2develop.provider.Book";
public static final Uri CONTENT_URI =
Uri.parse("content://"+ PROVIDER_NAME + "/books");
public static final String _ID = "_id";
public static final String TITLE = "title";
public static final String ISBN = "isbn";
private static final int BOOKS = 1;
private static final int BOOK_ID = 2;
private static final UriMatcher uriMatcher;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "books", BOOKS);
uriMatcher.addURI(PROVIDER_NAME, "books/#", BOOK_ID);
}
any help is greatly accepted... thanks in advance
Static initialization code runs in textual order. This includes static fields as well as static blocks.
PROVIDER_NAME
is therefore already initialized when the static block executes