开发者

Getting rid of annoying warnings in Eclipse

The following code, where Config.PREFERENCES_ENABLE is a开发者_Go百科 static final boolean, produces a compiler warning (on all the code after the if block and the warning is "Dead Code"), and trying to use @SuppressWarnings("all") on it produces a syntax error. I know I'm being a bit OCD here, but you know how it is.

void displayPreferences() {

    if(!Config.PREFERENCES_ENABLED) return;

    startActivityForResult(
            new Intent(this, PrefsActivity.class),
            PREFERENCE_ACTIVITY);
}


Aside from changing the Eclipse compiler preferences, there are a few other options you can choose:

  1. Specify @SuppressWarnings("unused") for the method to suppress the specific warning.
  2. Remove the final from Config.PREFERENCES_ENABLED to satisfy the compiler.
  3. Store the preference in some external file and read that at runtime.

The first option at least allows you to keep the unused compiler warning enabled, which might be useful in other parts of code later. The second option might not be desirable if you're really concerned that something might change it during runtime (or perhaps that's even desirable). The third option has the benefit of not hard coding the preference and allowing it be configured in an external file that's easier to change without recompiling code.


Window > Preferences > Java > Compiler > Errors/Warnings > Unused Code

I think. I don't have Eclipse open in front of me. You can use the filter box in the preferences dialog to find it, too.


Why don't you just write it like this?

void displayPreferences() {
    if (Config.PREFERENCES_ENABLED) {
        startActivityForResult(
            new Intent(this, PrefsActivity.class),
            PREFERENCE_ACTIVITY);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜