Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 私有静态变量出错_Java - Fatal编程技术网

Java 私有静态变量出错

Java 私有静态变量出错,java,Java,我有一个类,它有一个私有静态变量 private static counter = 0; 但当我编译它时,它给出了一个错误: java:3:预期 private static counter = 0; ^ 有人知道为什么会这样吗?我不明白它怎么了 以下是全部代码: public class Car { private static counter = 0; private String name; public Car()

我有一个类,它有一个私有静态变量

private static counter = 0;
但当我编译它时,它给出了一个错误:

java:3:预期

private static counter = 0;
                      ^
有人知道为什么会这样吗?我不明白它怎么了

以下是全部代码:

public class Car
{
    private static counter = 0;
    private String name;

    public Car()
    {
    name = "car" +counter;
    counter++;
    }
}

您需要指定变量的类型

private static int counter = 0;

你忘了打字了。看来你是说

private static int count=0