javaandroidfinalaide-ide

Why can I re-assign a new value to a final variable in Android AIDE?


How this is possible? How am I able to change variables marked as final?

public class Main
{
    public static void main(String[] args)
    {
        final int NUM;

        NUM = 22;
        NUM = 33;

        System.out.println(NUM);
    }

}

I was using AIDE app in Android... it compiled successfully and printed 33.


Solution

  • If you can do this, it is a bug in the AIDE app. You should report it to the developers. A JLS compliant implementation of Java does not allow reassignment of final variables (like this).

    If you want to give them a JLS specification reference to support your bug report:

    JLS 4.12.4 final variables

    A variable can be declared final. A final variable may only be assigned to once. It is a compile-time error if a final variable is assigned to unless it is definitely unassigned immediately prior to the assignment (§16 (Definite Assignment)).