Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "implicit anonymous class parameter" mean in this context?

In Android Studio, the following code has the variable commandBytes colored to indicate an "implicit anonymous class parameter":

public boolean writeCommand( byte[] commandBytes ) {
if( writeCommandInProgress.compareAndSet( false, true ) ) { 
    writeSubscription = bleDevice
            .establishConnection( asBleServiceRef, false )
            .flatMap( rxBleConnection -> rxBleConnection.writeCharacteristic( asInputCharId, commandBytes) )
            .subscribe( 
                    characteristicValue -> { 
                        writeCommandInProgress.set( false ); 
                        if( !Arrays.equals( characteristicValue, commandBytes ) )
                            Log.d( LOG_TAG, "Data read back from writeCommand() doesn't match input");
                    },
                    throwable -> Log.d( LOG_TAG, "Error in writeCommand: " + commandBytes.toString() + "; " + throwable.getMessage() )
            );
    return true;
    } else return false;
}

I can't figure out what this means. The description on JetBrains' help site isn't really helping: "That's a local variable which is used by an anonymous or local class inside the method and thus becomes a field of the anonymous class." How does this apply? Is it something I need to worry about?

like image 938
Robert Lewis Avatar asked Feb 17 '17 15:02

Robert Lewis


1 Answers

The colouring indicates the parameter or local variable is declared outside and used inside an anonymous class (or lambda in this case). To make this possible the javac compiler will create a synthetic field in the anonymous class to store the variable in. You can see this by inspecting the bytecode (View > Show Bytecode).

This is not something to worry about, it is not a warning, it's just a colouring of the syntax to provide information. It does mean the variable is implicitly or explicitly final, so it's not possible to reassign it without breaking compilation.

like image 110
Bas Leijdekkers Avatar answered Oct 22 '22 10:10

Bas Leijdekkers