Java |构造函数|问题8

null

class Test
{
static int a;
static
{
a = 4 ;
System.out.println ( "inside static block" );
System.out.println ( "a = " + a);
}
Test()
{
System.out.println ( "inside constructor" );
a = 10 ;
}
public static void func()
{
a = a + 1 ;
System.out.println ( "a = " + a);
}
public static void main(String[] args)
{
Test obj = new Test();
obj.func();
}
}


(A)

inside static block
a = 4
inside constructor
a = 11

(B) 编译错误 (C) 运行时错误 (D)

inside static block
a = 4
inside constructor
a = 5

(E)

inside static block
a = 10
inside constructor
a = 11

答复: (A) 说明: 静态块在构造函数之前被调用。因此,在类测试的对象创建时,调用静态块。静态变量a=4。 然后调用构造函数Test(),它指定a=10。最后,函数func()增加其值。 这个问题的小测验 如果你在上面的帖子中发现任何错误,请在下面发表评论

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享