JAVA 最基础的问题~~~~~

来源:百度知道 编辑:UC知道 时间:2024/05/21 12:32:03
class Rodent
{
Rodent r=new Rodent();
Rodent()
{
System.out.println("Greated Rodent" );
}

public static void main(String[] args)
{
Rodent rod=new Rodent();

}

}

这个例子为什么不能运行?
第3句如果改为 static Rodent r=new Rodent(); 可以正常运行
但是我想知道为什么这样不行?

非静态变量只能在创建对象的时候实例化

class Rodent
{
Rodent r=new Rodent(); //这句删掉
Rodent()
{
System.out.println("Greated Rodent" );
}

public static void main(String[] args)
{
Rodent rod=new Rodent();

}

}

Rodent r=new Rodent(); 错误
类还没说明完呢,怎么实例化

class Rodent ---public class Rodent

可能main定义有误