In one of the standard examples - SampleSyncAdapter - there is such a fragment:

public static boolean authenticate(String username, String password, Handler handler, **final Context context**) { **final HttpResponse resp;** **final ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();** params.add(new BasicNameValuePair(PARAM_USERNAME, username)); params.add(new BasicNameValuePair(PARAM_PASSWORD, password)); HttpEntity entity = null; try { entity = new UrlEncodedFormEntity(params); } catch (**final UnsupportedEncodingException e**) { // this should never happen. throw new AssertionError(e); } **final HttpPost post = new HttpPost(AUTH_URI);** ... 

Further, some variables are marked as final. What for?

    4 answers 4

    Matter is not in a static method, final can use everywhere. There are 2 reasons for using final :

    1. When the compiler encounters the final keyword, it tries to do code optimization, meaning that the value of this variable will not change further. Some pennies (saving the stack or speed) by this optimization can be won.
    2. In addition, the programmer can consciously use the word final in order to control its value already at the compilation stage or not. That is, if there is an attempt to modify the value assigned during initialization, the compiler will generate an error. Some developers think that it is ice.
    3. This variable will be captured by an anonymous class (true for versions less than 8). In order to capture variables in context, they must be marked as final. Starting with version 8, the compiler itself can determine whether a variable is immutable (effectively final) and the keyword becomes optional.

      An example to demonstrate what Barmaley said (paragraph 2):

       final int a = 1; a = 2; // нельзя final int b; b = 3; // можно b = 4; // нельзя 
      • Those. You can assign a value to the final variable for the first time (for example, by pressing a button), but not the second time, right? - researcher

      If a variable is declared final, its value does not change in runtime. This allows the programmer to be sure that the value of the variable will not change i. a variable is a constant, for example, if you need a pi number - you don’t need a pi number to change. (of course it can be taken from the class Math, if the task requires it).

      More specifically:

      The value of the variable file is assigned only 1 time either immediately upon declaration or in the constructor. Even if you try to assign a variable the same value that is already in it, the compiler will curse anyway.

        The final keyword is also important in terms of the Java Memory Model .

        For clarity, I will show on the code:

         class A { private Object field = new Object(); } 

        If this object is created in one stream and transmitted in another, then the JMM does not guarantee that the field will not be null . To avoid this situation is used final . This gives some guarantees, without using synchronization, to see a fully initialized object in different threads.