Variables with underline

后端 未结 6 1586
轮回少年
轮回少年 2020-12-28 12:23

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

相关标签:
6条回答
  • 2020-12-28 12:54

    It could be a sign of "Reassigned parameter"

    0 讨论(0)
  • 2020-12-28 12:56

    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.

    0 讨论(0)
  • 2020-12-28 12:56

    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...
    
    }
    
    0 讨论(0)
  • 2020-12-28 13:08

    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).

    0 讨论(0)
  • 2020-12-28 13:12

    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.

    0 讨论(0)
  • 2020-12-28 13:15

    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.

    0 讨论(0)
提交回复
热议问题