当我声明带有静态的变量时,我得到了一个非法的表达式开始?


static int basketballPlayer; 
static int activebasketballPlayers;
static int legendbasketballPlayers;

非法开始表达

在 Java 中,static修饰符适用于字段方法。请参阅了解类成员,其中简要讨论了修饰符的使用。

class Foo {
    static int foo; // okay - static applied to field
}

但是static不能与 Java 中的局部变量一起使用

class Bar {
    void x () {
        static int bar; // fail - static cannot be applied to local variables
    }
}

上面的结果是熟悉的编译器错误:

error: illegal start of expression
    static int bar;

只有在类开始时声明字段时,才能使用 static。如果尝试在方法中声明这些,则不能使用 static。

我可以向你推荐这个,它应该可以帮助你理解Java:http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html

最新更新