(7)关于以下application的说明,正确的是( )
1. class StaticStuff
2. {
3. static int x=10;
4. static { x+=5;}
5. public static void main(String args[ ])
6. {
7. System.out.println(“x=” + x);
8. }
9. static { x/=3;}
10. }
A、4行与9行不能通过编译,因为缺少方法名和返回类型
B、9行不能通过编译,因为只能有一个静态初始化器
C、编译通过,执行结果为:x=5
D、编译通过,执行结果为:x=3这是那个题
答案是B
我把下面的代码拷贝出来,运行结果是C,求解class StaticStuff
{
static int x=10;
static { x+=5;}
public static void main(String args[])
{
System.out.println("x=" + x);
}
static { x/=3;}
}
解决方案 »
- 求各位给下面的程序支点招……
- 怎样设计boolean prime(int n)方法,可用来判别n是否为质数,还有异常处理。
- PrepareStatement问题,给分速度
- jdbc中有没有那个类可以知道数据库中某个表否个字段是不是主键
- 也是Button的大小问题
- 关于pickList的问题?(在线等)
- 关键字: oracle,存储过程,数据库,查询,动态sql包,数组,参传,jdbc 1
- 在 JBuilder 6 中,如何修改设置使得编译时支持 -deprecation 选项?
- whitenshen
- 关于java.lang.ClassNotFoundException的mysql数据库连接程序
- 数据库查询出记录封装到对象中
- nextInt*()函数源代码的解释 求助
static { x+=5;} // 15
public static void main(String args[]) {
System.out.println("x=" + x); }
static { x/=3;} // 5
}
按顺序载入static块。
static int x = 10;
static {
x += 5;
System.out.println("static x += 5, x = " + x);
} public static void main(String args[]) {
System.out.println("in main");
System.out.println("x=" + x);
} static {
x /= 3;
System.out.println("static x /= 3, x = " + x);
}
}
static{ //这是静态块 }//静态块只有在类第一次被加载的时候进行初始化,不会进行第二次加载
//注意下面写法
public static void main(String[] args){
static{ int i=0; }//这种写法是会编译出错的
}
//注意静态关键字的含义,static所修饰的成员只属于类,在方法中是没有static的引用的
static修饰的成员在堆中开辟了唯一的空间,不会进行第二次开辟