无法在静态方法中引用非静态成员(包括方法和变量)
之所以会报Cannot make a static reference to the non-static field email这个错,
是因为在静态方法中,不能直接访问非静态成员(包括方法和变量)。
因为,非静态的变量是依赖于对象存在的,对象必须实例化之后,它的变量才会在内存中存在。
因此,这个东西解决起来特别简单,第一种,可以把变量改成静态的。第二种,先实例化对象,然后使用对象名.变量名来调用即可。
==============错误的:===================
public class BB{
int a[] =new int[10];
public static void main(String[] args) {
System.out.println(a[1]);
}
}
int a[] =new int[10];
public static void main(String[] args) {
System.out.println(a[1]);
}
}
======================正确的:===============
public class BB{
int a[] =new int[10];
public static void main(String[] args) {
BB bb= new BB();
System.out.println( bb.a[1]);
}
}
int a[] =new int[10];
public static void main(String[] args) {
BB bb= new BB();
System.out.println( bb.a[1]);
}
}