java中如果静态调用了非静态方法会报什么错误?

请附上试验代码和错误代码.

你们都理解错了。

我的意思是这样,php中可以这么用。

<?php
class test{
function test2(){
}
}
test::test2();

我想知道其他语言能不能

首先楼上的说法是错误的。
java中如果静态调用了非静态方法有两种情况:
一种是间接调用,例如:
public class Apple {
public static void sayHello()//静态方法
{
/./建立一个对象后,然后调用对象的非静态方法,这样是正确的,没有任何错误
Apple apple=new Apple();
apple.print();
}
public void print()
{
System.out.println("Apple");
}
}
第二种是直接调用,不用建立对象
例如:
public class Apple {
public static void sayHello()//静态方法
{
//直接调用非静态方法,这样会编译错误,错误原因是:cannot make a static reference to the non-ststic method
print();
}
public void print()
{
System.out.println("Apple");
}
}
这是因为,非静态的方法必须要建立一个对象,然后用这个对象来调用。并且在非静态方法中都隐藏着一个this对象,在静态方法中却没有this对象,这是静态和非静态的本质区别
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-03-06
public static void makeInner()
{
Inner in = new Inner();
in.seeOuter(); 《===此处提示:无法再静态上下文中引用非静态变量 this
}
这里的makeInner就是静态(static),而里面建立了非静态变量in,所以通不过本回答被网友采纳
第2个回答  2011-03-06
自己试试看呗
第3个回答  2011-03-06
编译通不过,
相似回答