这是jdk的编译顺序造成的,在同样优先级别的变量在编译中的顺序是根据代码中的顺序来的
可能这样讲有点抽象,说明白一点,在使用id和nextid变量时,还没有为其分配空间,做如下改动即可通过编译:
private int id;
private static int nextId;
//这里出错
{
id=nextId;
nextId++;
}
//这里出错
static
{
Random generator=new Random();
nextId=generator.nextInt(10000);
}
可能这样讲有点抽象,说明白一点,在使用id和nextid变量时,还没有为其分配空间,做如下改动即可通过编译:
private int id;
private static int nextId;
//这里出错
{
id=nextId;
nextId++;
}
//这里出错
static
{
Random generator=new Random();
nextId=generator.nextInt(10000);
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货