think in java中有一处讲到尽管没有“显式地使用static关键字,构造器实际上也是静态方法。”
但是静态方法不是不能对非静态字段进行引用的吗?
但是我试过了好像可以呀。
这是为什么呢?
code:
class StaticArea{
int i;
StaticArea(){
i=3;
}
}
public class TestStatic {
public static void main(String[] args){
StaticArea sa=new StaticArea();
System.out.println(sa.i);
}
}
求解释...
...全文
3668打赏收藏
java中静态方法调用非静态变量的问题
think in java中有一处讲到尽管没有“显式地使用static关键字,构造器实际上也是静态方法。” 但是静态方法不是不能对非静态字段进行引用的吗? 但是我试过了好像可以呀。 这是为什么呢? code: class StaticArea{ int i; StaticArea(){ i=3; } } public class TestStatic { public static void main(String[] args){ StaticArea sa=new StaticArea();
尽管没有显式地使用static关键字,构造器实际上也是静态方法
主要看后面的描述,其实和说private methods是final的那段一样,都是说的意义,而不是结构
So the first time an object of type Dog is created, or the first time a
static method or static field of class Dog is accessed, the Java interpreter must
locate Dog.class, which it does by searching through the classpath.