public class Test{ static { System.out.println("Outer static"); } static class Inner{ static{ System.out.println("Inner static"); } public static int a; } public int b; public static void main(String[] args){ new Test(); } } 输出结果是“Outer static”,说明没有初始化静态内类中的静态成员
找到原因了,SL-275里面说:Inner classes that are declared static automatically become toplevel classes. Static inner classes are not created relative to an object of the outer class. Therefore, methods of a static inner class do not have access to the outer class scope.声明为 static 的内部类自动成为toplevel类。静态内部类不需要由外部类德实例对象引用。因此,静态内部类的方法没有权限访问外部类作用域。那么不声明为静态的内部类呢? Inner classes cannot declare any static members; only top-level classes can declare static members. Therefore, an inner class requiring a static member must use one from the top-level class. 内部类不可以声明任何的静态成员,只有toplevel类可以声明静态成员。因此,一个内部类如果需要静态成员必须使用来自顶级类的静态成员。关键部分: Note – When a top-level class in compiled, the inner class is compiled as well. A .class file is created for it with a name that takes the form: OuterClass$InnerClass. For example, the Inner inner class is compiled to the file: Outer$Inner.class. 注意:当顶级类编译时,内部类同时被编译。
JavaRookie(Java Rookie)请看 class A9 { public static void main(String[] args) { System.out.println(Outer.Inner.a); }
static class Outer { private static int i; static { System.out.println("outer"); } static class Inner { public static int a;
static { System.out.println("inner"+i); } }
} } 输出outer inner0 0 似乎说明只对最后一个被引用到的静态类进行初始化
楼上的请看 SL-275: The static block code executes only once, when the class is loaded. Different static blocks within a class are executed in the order of their appearance in the class. 我觉得你的代码先引用outer.inner.a,初始化inner static block,由于引用outer.i所以先初始化outer加载outer.i,然后加载inner.a
java A9时,编译器会载入A9.class ,接着会初始化类A9的静态成员,这我知道
但他会不会初始化静态内类中的成员呢
static {
System.out.println("Outer static");
}
static class Inner{
static{
System.out.println("Inner static");
}
public static int a;
}
public int b;
public static void main(String[] args){
new Test();
}
}
输出结果是“Outer static”,说明没有初始化静态内类中的静态成员
static {
System.out.println("Outer static");
}
"
是什么意思?
把语句加在static{}中间有什么作用啊?
我想可能是这样的,java A9时并没有载入A9.class,而第一次引用到静态类inner时,A9$inner.class被载入,所以i被初试为0。
如
class Ex1
{
static class inner
{
static A j=new A(1);
static {
System.out.println("A(2)");
}
};
public static void main(String[] args)
{
Object a=Ex1.inner.j;
}
};
class A
{
A(int i)
{
System.out.println("A()"+i);
}
};
输出为
A()1
A(2)
似乎合乎我的想法
benwang6()的代码没有去引用静态内类,所以没有输出"outer static"
不知道想的对不对,还请高手指点
class Ex1
{
static class inner
{
static {
System.out.println("A(2)");
}
static A j=new A(1);
}
public static void main(String[] args)
{
Object a=Ex1.inner.j;
}
}输出结果就成了:
A(2)
A()1
public class A9 {
public static void main(String[] args) {
System.out.println(Outer.Inner.a);
}
static class Outer {
static {
System.out.println("outer");
} static class Inner {
public static int a;
static {
System.out.println("inner");
}
}
}
}输出的结果为:
inner
0
说明根本就没有加载outer类,而是直接引用inner类
当然这个例子里的outer和inner都是static,可能改动后又会有不同
classes. Static inner classes are not created relative to an
object of the outer class. Therefore, methods of a static inner class do not have access to the outer class scope.声明为 static 的内部类自动成为toplevel类。静态内部类不需要由外部类德实例对象引用。因此,静态内部类的方法没有权限访问外部类作用域。那么不声明为静态的内部类呢?
Inner classes cannot declare any static members; only top-level
classes can declare static members. Therefore, an inner class
requiring a static member must use one from the top-level class.
内部类不可以声明任何的静态成员,只有toplevel类可以声明静态成员。因此,一个内部类如果需要静态成员必须使用来自顶级类的静态成员。关键部分:
Note – When a top-level class in compiled, the inner class is compiled as well. A .class file is created for it with a name that takes the form: OuterClass$InnerClass. For example, the Inner inner class is compiled to the file: Outer$Inner.class.
注意:当顶级类编译时,内部类同时被编译。
class A9 {
public static void main(String[] args) {
System.out.println(Outer.Inner.a);
}
static class Outer {
private static int i;
static {
System.out.println("outer");
} static class Inner {
public static int a;
static {
System.out.println("inner"+i);
}
}
}
}
输出outer
inner0
0
似乎说明只对最后一个被引用到的静态类进行初始化
SL-275:
The static block code executes only once, when the class is loaded. Different static blocks within a class are executed in the order of their appearance in the class.
我觉得你的代码先引用outer.inner.a,初始化inner static block,由于引用outer.i所以先初始化outer加载outer.i,然后加载inner.a