静态内部类
来源:百度知道 编辑:UC知道 时间:2024/06/14 02:56:45
在静态内部类中的静态方法,能够访问静态内部类中的非静态成员或方法吗??
那在程序开始加载的时候,静态成员应该已经分配了空间,那么静态内部类呢?静态内部类中的非静态方法和成员呢? 要是说,静态内部类也是一开始就分配好了空间,那么里面的非静态成员和方法也已经分配了吗?
那在程序开始加载的时候,静态成员应该已经分配了空间,那么静态内部类呢?静态内部类中的非静态方法和成员呢? 要是说,静态内部类也是一开始就分配好了空间,那么里面的非静态成员和方法也已经分配了吗?
一试便知,附测试代码及相关错误提示.
public class NomalClass {
static class InnerClass {
int i;
void nonstatic() {
}
static void test() {
i = 10;
// 错误提示
// Cannot make a static reference to the non-static method
// nonstatic() from the type NomalClass.InnerClass
nonstatic();
// 错误提示
// Cannot make a static reference to the non-static method
// nonstatic() from the type NomalClass.InnerClass
}
}
}
可以
不能,静态内部类无论在哪都只能访问静态的成员变量或方法
静态内部类只能访问静态的成员变量或方法 因此不可以
不可以
把书翻开看一遍静态内部类介绍就一目了然