Android Studio reports "Unreachable code" with cross-comparison

I am doing something that should be trivial - getting the enum value from the property and comparing it with the constant of this enum in the if . However, Android Studio claims that the true code is unreachable and does not compile.

Block:

 if (ScanState.getScanMode() != ScanState.ScanModeEnum.SCAN_IDLE) { //We're already scanning, but user wants to stop. stopScanning(); } else { ScanState.setScanMode(newMode); restartScan(); buttonFlashMode = btnMode; buttonFlasher(); } 

where in the optional ScanState class I have:

 public static ScanModeEnum getScanMode() { return scanMode; } public static void setScanMode(ScanModeEnum scanMode) { ScanState.scanMode = scanMode; } public enum ScanModeEnum { SCAN_IDLE, SCAN_PERSON, SCAN_BIKE, SCAN_SEARCH } private static ScanModeEnum scanMode = ScanModeEnum.SCAN_IDLE; 

The options I tried that Android Studio claim will be evaluated as false

 if(ScanState.getScanMode() == ScanState.ScanModeEnum.SCAN_IDLE) if(ScanState.getScanMode().compareTo(ScanState.ScanModeEnum.SCAN_IDLE)!=0) if(ScanState.ScanModeEnum.SCAN_IDLE == ScanState.ScanModeEnum.SCAN_IDLE) if(ScanState.ScanModeEnum.SCAN_IDLE.equals(ScanState.ScanModeEnum.SCAN_IDLE)) 

I am new to Java (more familiar with C #), but the answer to this question suggests that my understanding of this sound. Is there some kind of stupid mistake I am making?

+6
source share
3 answers

Good grief. Having made a separate method, proposed and discovered the problem elsewhere, I looked further at the code. Full method:

 public void onScanButtonPress(@ButtonFlashMode int button) { ScanState.ScanModeEnum newMode; @ButtonFlashMode int btnMode = 0; switch (button) { case FLASH_BIKE: newMode = ScanState.ScanModeEnum.SCAN_BIKE; btnMode = FLASH_BIKE; case FLASH_PERSON: newMode = ScanState.ScanModeEnum.SCAN_PERSON; btnMode = FLASH_PERSON; default: //Unhandled. return; } if (ScanState.getScanMode() != ScanState.ScanModeEnum.SCAN_IDLE) { //We're already scanning, but user wants to stop. stopScanning(); } else { ScanState.setScanMode(newMode); restartScan(); buttonFlashMode = btnMode; buttonFlasher(); } } 

Since I forgot to put break statements in switch cases, it will always be returned before the if is evaluated. Therefore, it will never evaluate to true , and therefore the error is true - if misleading, because it implies (at least for me!) That the if gets an estimate. Thanks for the comments, and I decided it was worth leaving (even though it was a really stupid mistake) because others could be caught by this.

0
source

Have you tried debugging this and checking that the block has never been reached?

I agree that this is a very strange situation. If it persists, I can recommend replacing enum with int constants and checking for them. This is not a real solution, but a more workaround, but at least it can unlock you at the moment.

0
source

EDIT: As @Bubletan and @MarkoTopolnik mentioned, this will not result in a compiler error. Leaving the answer as documentation that will NOT lead to this error.

Are you calling your setScanMode code setScanMode ? (outside this block). The compiler can detect that the static variable scanMode never changes, therefore ScanState.getScanMode() always ScanState.ScanModeEnum.SCAN_IDLE , therefore the code is not available. Try calling setScanMode somewhere in your code (with a value other than ScanState.ScanModeEnum.SCAN_IDLE ) and see if this error goes away.

0
source

All Articles