3
votes

I got a doubt regarding the content provider.
Everytime when i am write the content provider, i am placing the URI MATCHER definition in the static brackets but the URI MATCHER is declared a private data member of the class. Only definition(new UriMatcher) is being placed in the static brackets.

Will anyone please let me know the reason. I tried googling but not able to find the answer. Me too will try please let me know if anyone knows already.

Thanks & Regards,
SSuman185

1

1 Answers

6
votes

It is a static initialization block. When you define a member or class variable the value must fit on a single line (even if you space it over more), and it cannot include complicated logic.

For member variables you can do this complicated initialization in the Constructor.

Essentially a static initialization block is a constructor for your class variables, allowing you to use more complicated expressions when initializing. It is only executed once, when the class is first loaded, no matter how many instances are created.

A private member just means the variable is not accessible to other classes, it is still accessible to the class itself. So the static initialization block only creates the URIMatcher once (when the class is loaded), no matter how many instances there are.