4

Is there any why to avoid "transform variables into final one element array"

public static String getSimSerialNumber(Context context) {
    final TelephonyManager tm =
        (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    final String mSimSerialNumber; // <----- (here the compiler ask for transformation)

    if (tm != null) {
      RxPermissions.getInstance(context)
          .requestEach(Manifest.permission.CAMERA, Manifest.permission.READ_PHONE_STATE)
          .subscribe(new Action1<Permission>() {
            @Override public void call(Permission permission) {
              if (permission.granted)  mSimSerialNumber = tm.getSimSerialNumber();
            }
          });
    }
    return null;
  }

Edit 1 If I accept the solution suggeseted by the compiler the code will be like following :

public static String getSimSerialNumber(Context context) {
    final TelephonyManager tm =
        (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    final String[] mSimSerialNumber = new String[1];

    if (tm != null) {
      RxPermissions.getInstance(context)
          .requestEach(Manifest.permission.CAMERA, Manifest.permission.READ_PHONE_STATE)
          .subscribe(new Action1<Permission>() {
            @Override public void call(Permission permission) {
              if (permission.granted)  mSimSerialNumber[0] = tm.getSimSerialNumber();
            }
      });
}
return null;
}

I think there is a way to avoid using an array (as we know array is used to store more than one element that's why we use array) to solve this problem ?

TooCool
  • 10,598
  • 15
  • 60
  • 85
  • 2
    Don't use an inner class. Then you can add a getter, and invoke it. If you must use an inner class, then you can't access a non-final local variable. – Elliott Frisch Aug 13 '16 at 14:10
  • 2
    It's not clear what you're asking - do you not understand why the IDE is suggesting that you do that, or are you asking what the risks are? – Jon Skeet Aug 13 '16 at 14:10
  • @JonSkeet another solution than `“transform variables into final one element array”` – TooCool Aug 13 '16 at 14:12
  • *Why* do you want an alternative solution to that? You're still not being clear. There's no point in saying what you don't want to do without saying *why* you don't want to do it. (Elliott has suggested one alternative, for example...) – Jon Skeet Aug 13 '16 at 14:15
  • @JonSkeet I think Elliott answer my question – TooCool Aug 13 '16 at 14:18
  • 1
    Well the only question you asked in the post is "Is there any why to avoid [...]" - that's not asking for a different solution, that's asking whether or not you should avoid the one that's being suggested. You haven't made any effort to clarify your post, as far as I can see... – Jon Skeet Aug 13 '16 at 14:19
  • @JonSkeet check my edit – TooCool Aug 13 '16 at 14:23
  • You still haven't said what you don't like about using the array. Are you trying to avoid creating a new object? Because you'll be creating a new object one way or another anyway. If you can't explain what you don't like about the array option, how are we going to know whether you'd have the same unspecified objection to other options? – Jon Skeet Aug 13 '16 at 14:24
  • 2
    BTW, topic starter mentions "solution suggested by the compiler", but indeed it's an IDE suggestion, not compiler, I guess. I mean not always IDE (even the best among of them) knows what you are doing and suggests the right stuff. – lospejos Aug 13 '16 at 14:26

0 Answers0