单台设计模式的一个小程序!求解!!

aplippy 2012-02-23 06:51:37
class TextSingle
{
private static final TextSingle onlyOne=new TextSingle();
private static int count=0;
TextSingle()
{
count++;
System.out.println("the objective has setuped and "+"count="+count);//运行时为count=1.[\b]
}
public static TextSingle getTextSingle()
{
return onlyOne;
}
public int createObjNumber()
{
System.out.println("count="+count);//[b]运行时为count=1?不解
return count;
}
}
public class scope31
{
public static void main(String[]args)
{
System.out.println("the number of the objective is "+TextSingle.getTextSingle().createObjNumber());//这个运行结果为count=1?
}
}

问题就是程序中的三个注释部分为什么最后一count运行出来是0呢??不解!!!
...全文
87 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
学无止境+ 2012-02-24
  • 打赏
  • 举报
回复
结贴了啦
MiceRice 2012-02-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 boy1324 的回复:]
果然如此!!谢谢 ldh911 !!对了,您能说说java中的对象实例化的过程吗?尤其是这种含多个static成员的类,怎么实例化。
[/Quote]

有static成员的情况下,根本还不需要实例化的时候就已经可以发生初始化动作了,都是在类定义被加载的时候就完成的,顺序是从上至下。

简单点说的话,如果你修改下count为public,然后在main函数中,这样:
public static void main(String[]args) {
System.out.println(TextSingle.count);
}
然后你就能看到输出结果:
the objective has setuped and count=1
0
aplippy 2012-02-23
  • 打赏
  • 举报
回复
谢谢HappyVeryGood!!这么说来,我是越来越明白了,谢谢指点!!
aplippy 2012-02-23
  • 打赏
  • 举报
回复
果然如此!!谢谢 ldh911 !!对了,您能说说java中的对象实例化的过程吗?尤其是这种含多个static成员的类,怎么实例化。
学无止境+ 2012-02-23
  • 打赏
  • 举报
回复
private static int count=0;
private static final TextSingle onlyOne=new TextSingle();
或者
private static final TextSingle onlyOne=new TextSingle();
private static int count;//这里不显示的赋值

就会有你满意的结果,,
原因:
静态变量的执行顺序是按照它们定义在类里面的先后顺序,按照从上到下来执行的

public class StaticVariableTest { private static StaticVariableTest svt = new StaticVariableTest();//语句(1) private static int count1;//语句(2) private static int count2 = 0;//语句(3) private StaticVariableTest(){//语句(4) count1++; count2++; } public static StaticVariableTest getInstance(){//语句(5) return svt; } public static int getCount1() { return count1; } public static void setCount1(int count1) { StaticVariableTest.count1 = count1; } public static int getCount2() { return count2; } public static void setCount2(int count2) { StaticVariableTest.count2 = count2; } public static void main(String[] args) { StaticVariableTest svt = StaticVariableTest.getInstance();//语句(6) System.out.println("count1:" + svt.getCount1());//语句(7) System.out.println("count1:" + svt.getCount2());//语句(8) } } 问题:当执行完语句(7)(8)时,打印结果分别是什么?为什么?

  解答:当执行完语句(7)时,打印结果是1,当执行完语句(8)时,打印结果是0。分析:程序执行从main方法开始,首先执行语句(6),调用getInstance方法,然而当它去调用这个方法的时候,它是一个静态的方法,在这个类里面定义了多个静态的成员变量。根据java初始化的顺序我们知道,对于静态的内容肯定是先执行的,也就是说在执行getInstance方法之前,肯定先执行private static StaticVariableTest svt = new StaticVariableTest();而且它是从上到下分别执行静态的内容。换句话说,这个程序首先执行private static StaticVariableTest svt = new StaticVariableTest();而这里面又要调用一个构造方法StaticVariableTest(),则去执行这个构造方法private StaticVariableTest(),执行这个构造方法时发现它里面的功能是将count1加1,将count2加1,而这个count1和count2是我们定义的int类型的静态变量。根据java对成员变量的默认值,count1和count2初始化的时候都被设置为0,当执行完构造方法后count1和 count2都等于1,这时StaticVariableTest这个对象就生成了,已经在内存里面存在了。接着赋给svt这个引用。那么svt这个引用指向的StaticVariableTest类型的对象,它里面的count1是1,count2也是1。接着发现下面一行private static int count1;它是一个静态的,那么它要执行这行代码,这行代码只是一个声明,但是没有赋值,接着它就跳过这行不再赋值了(究其原因是因为count1已经被赋值了,已经被加1了,也就是count1为1)。当我执行private static int count2 = 0;时发现count2也是一个静态变量,而且有一个显示的去赋值的这样一个动作。我们知道count2已经被赋值1了,但是这儿有一个显示的赋值的动作,就把count2的值由1改变成了0。这个就是调用getInstance方法时程序的执行流程:语句(6)、语句(5)、语句(1)、语句(4)、语句(2)、语句(3)

  思考:如果将语句(2)和语句(3)放在语句(1)前面,当执行完语句(7)时,打印结果是1,当执行完语句(8)时,打印结果是1,想想是为什么?

  总结:静态变量的执行顺序是按照它们定义在类里面的先后顺序,按照从上到下来执行的。
共同学习,共同进步。。。
原文地址:http://www.chubu1.net/newscenter.asp?tid=85
MiceRice 2012-02-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 boy1324 的回复:]
最后一个输出的结果是count=0;但是我认为该是count=0;才对啊,count不是静态的吗??
[/Quote]


啊,忽略了一个问题,从赋值顺序上来说:
private static int count=0;
是在:
private static final TextSingle onlyOne=new TextSingle();
之后执行的。


所以执行顺序是:
1、private static final TextSingle onlyOne=new TextSingle();
2、TextSingle() {
count++;
System.out.println("the objective has setuped and "+"count="+count); // count = 1
}
3、private static int count=0; // 这里被清0了
4、TextSingle.getTextSingle().createObjNumber()
5、public int createObjNumber() {
System.out.println("count="+count);// 仍然是0
return count;
}
6、System.out.println("the number of the objective is "+.... // 只能是0


楼主可以把前面两句话位置换下,马上真相大白:
private static int count=0;
private static final TextSingle onlyOne=new TextSingle();
aplippy 2012-02-23
  • 打赏
  • 举报
回复
最后一个输出的结果是count=0;但是我认为该是count=0;才对啊,count不是静态的吗??
MiceRice 2012-02-23
  • 打赏
  • 举报
回复
从执行顺序分析,应该是:
1、private static final TextSingle onlyOne=new TextSingle();
2、TextSingle() {
count++;
System.out.println("the objective has setuped and "+"count="+count);//运行时为count=1.[\b]
}
3、TextSingle.getTextSingle().createObjNumber()
4、public int createObjNumber() {
System.out.println("count="+count);//[b]运行时为count=1?不解
return count;
}
5、System.out.println("the number of the objective is "+....

那么看起来运行结果应该是三个 1 ?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧