最终不能公开静态

Coa*_*rse 0 java access-modifiers

我确信这是一个非常简单的解释,它会让我感到愚蠢,但我无法弄清楚. 第18行的Pastebin:

public static boolean loadTextures() {
    try {
        final Texture STONE = loadPNG("main\\textures\\stone.png"); // This line here I can't do public static final...         
    } catch (IOException e) {
         return false;
    }
    return true;
}
Run Code Online (Sandbox Code Playgroud)

我想STONE成为public static final,但是eclipse说只是final一个合法的修饰语.我如何宣布public static final变量?

Era*_*ran 8

您不能在方法中声明静态变量,因为方法只有局部变量.

将它移到方法之外.

改变这个:

public static boolean loadTextures() {
                try {
                        final Texture STONE = loadPNG("main\\textures\\stone.png"); // This line here I can't do public static 
Run Code Online (Sandbox Code Playgroud)

对此:

public static final Texture STONE = loadPNG("main\\textures\\stone.png");
public static boolean loadTextures() {
                try {
Run Code Online (Sandbox Code Playgroud)