Home > Back-end >  Notification saying "Reassigned local variable" in Android Studio
Notification saying "Reassigned local variable" in Android Studio

Time:12-23

My string is getting underlined, with a notification saying "Reassigned local variable". I have worked out that this happens any time that the string is modified, but I am not sure why this notification is coming up.

It doesn't appear do be causing any errors, and nothing is showing under the "problems" tab, so I'm not sure if it is something I should change or if I should just ignore it.

Below is a simplified version of the code.

Canvas canvas;
Paint p = new Paint();

String value = "a"; //'value' here becomes underlined

if(someCondition()) value  = "bcdefghijklmnop"; //'value' here becomes underlined

canvas.drawText(value, 50, 50, p);

CodePudding user response:

It is just a notification, probably because sometimes reassigned local variables can cause problems for later operations. In your case it is totally legit to reassign, just be aware that it will create several new objects.

If you would like to prevent this, you could work with a StringBuilder here that you convert to a String whenever necessary, but this also creates a new String object, so if you don't use this in a loop usually it is not necessary to change anything (and for the string manipulation in a loop you probably will get a warning, a stronger sign for a potential problem in your code than this actual notification).

  • Related