I\'m getting an underline in some variables on Android Studio (in this case on the \'position\' variable). I think it\'s not an error because the application runs perfectly
It could be a sign of "Reassigned parameter"
I believe the underlined variables are representative of constants (final or effectively final), because in my experience I only see this decoration when I declare a final
object for use inside an anonymous class. I can't seem to find it in the documentation, though.
This means the variable was declared outside the current method. For example, in this case, position
is probably declared as a class member outside the new DialogInterface.OnClickListener()
, in the class where you're implementing the onItemLongClick()
method.
They are declared like this:
public class MyClass{
private int position;
// Other code...
}
I've found the answer for this question here.
The decoration is a syntax highlighting preference. Take a look at File > Settings > Editor > Color Scheme > Java/Kotlin
In the case of Java, you can find this effect for example at Parameters > Implicit anonymous class parameter
. It's the checkbox Effects
.
The same with Kotlin at Properties and Variables > Var (mutable variable, parameter or property)
.
If you know what is the side effect in programming then it will be easy for you. To protect your variable from the side effect, the IDE shows the underline as a warning to you. Which is sometimes very helpful to reduce logical bugs in your code.
It may be because a immutable variable is subjected to modification. Like reassigning a String or trying to modify a final declared variable.
String buffer = "";
buffer = buffer + "new string";
Will underline the buffer, since string are of immutable Objects.