举一个static方法中不能调用非静态方法的例子给我看

来源:百度知道 编辑:UC知道 时间:2024/05/16 02:54:27
java

这个程序中,在静态方法f1中调用了非静态方法f2,产生了错误
non-static method f2() cannot be referenced from a static context
f2();

class test
{
public static void main(String []args)
{
A X=new A();
x.f1();

}
}

class A
{
public static void f1()
{
System.out.println("static");
f2();
}

public void f2()
{
System.out.println("no static");
}
}