class SuperEx {
static String quanju="haha";
public SuperEx(String a,String b) {
}public void aMethod() {
System.out.println(quanju);
}
}public class NewSuper extends SuperEx {
public NewSuper(String a,String b) {
super(a,b);
}
public static void main(String args []) {
SuperEx c = new SuperEx("Hi","Tom");
SuperEx d = new NewSuper("Hi","Bart");
super.aMethod();//如果把这行删除就不出错,这行出:“无法从静态上下文中引用非静态变量 super
d.aMethod();
}
public void aMethod() {
System.out.println(quanju); }
}
static String quanju="haha";
public SuperEx(String a,String b) {
}public void aMethod() {
System.out.println(quanju);
}
}public class NewSuper extends SuperEx {
public NewSuper(String a,String b) {
super(a,b);
}
public static void main(String args []) {
SuperEx c = new SuperEx("Hi","Tom");
SuperEx d = new NewSuper("Hi","Bart");
super.aMethod();//如果把这行删除就不出错,这行出:“无法从静态上下文中引用非静态变量 super
d.aMethod();
}
public void aMethod() {
System.out.println(quanju); }
}
1、在静态方法中只能直接调用同类中其他的静态成员,而不能直接访问类中的非静态成员。原因是对于非静态的方法和变量,需要先创建类的实例对象后才可使用,而静态方法在使用前不用创建任何对象;
2、 静态方法不能以任何方式引用this和super关键字;
3、 main()方法是静态的,因此,JVM在执行main方法时不创建main方法所在的类的实例对象,因此在main()方法中,我们不能直接访问该类中的非静态成员,必须创建该类的一个实例对象后,才能通过这个对象去访问类中的非静态成员。
静态方法不能以任何方式引用this和super关键字;《---呵呵 我刚在百度查的,这样我好像就知道为什么出错了
然后,再用子类的对象调用这个方法.就不会出现了.我也是菜鸟哦.因为这个问题我遇见过N次了.呵呵.
需要等对象产生后,再根据对象的地址与偏移地址才能获得非静态方法的调用句柄。如果在静态方法中直接调用非静态方法,实际只是取到了一个内存偏移地址,并非实际内存地
址,这样就找不到调用句柄。