代码如下:
public class StaticTest{
public void sayHello(){
System.out.println("Hello");
}
public static void Test(){
sayHello();//调用非static方法。这句话其实就是相当于 this.say(); this是指调用被方法的对象
System.out.println("static test");
}
public static void main(String[] args){
StaticTest.test();
}
}
结果:
static修饰的方法是属于类的,可以用 “类.方法”形式调用的,此时系统并没有创建对象,系统无法识别非static的方法的对象,报错!