这段代码奇怪的输出
by
at 2010-12-15 20:33:28
original http://www.javaeye.com/topic/843250
class singlen {
private static singlen obj=new singlen();
public static int count1;
public static int count2=0;
private singlen(){
count1++;
count2++;
}
public static singlen getInstence(){
return obj;
}
}
class dd{
public static void main(String[] args){
singlen s=singlen.getInstence();
System.out.println("count1=="+s.count1);
System.out.println("count2=="+s.count2);
}
}
上边代码将输出:
count1==1
count2==0
首先初始化静态变量,再执行构造函数,怎么会奇怪的输出
<br><br>
作者: <a href="http://zkun001.javaeye.com">zkun001</a>
<br>
声明: 本文系JavaEye网站发布的原创文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!
<br><br>
<span style="color:red">
<a href="http://www.javaeye.com/topic/843250" style="color:red">已有 <strong>8</strong> 人发表回复,猛击->><strong>这里</strong><<-参与讨论</a>
</span>
<br><br><br>
JavaEye推荐