public class Singleton {
(1)private static Singleton obj = new Singleton();
(2)public static int counter1;
(3)public static int counter2 = 1;
(4)private Singleton(){
counter1++;
counter2++;
}
(5)public static Singleton getInstance(){
return obj;
}
}public class MyMain { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Singleton obj = Singleton.getInstance();
System.out.println("obj.counter1=="+obj.counter1);
System.out.println("obj.counter2=="+obj.counter2); }}请说出(1)(2)(3)(4)(5)的运行顺序和运行结果,并说出原因
(1)private static Singleton obj = new Singleton();
(2)public static int counter1;
(3)public static int counter2 = 1;
(4)private Singleton(){
counter1++;
counter2++;
}
(5)public static Singleton getInstance(){
return obj;
}
}public class MyMain { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Singleton obj = Singleton.getInstance();
System.out.println("obj.counter1=="+obj.counter1);
System.out.println("obj.counter2=="+obj.counter2); }}请说出(1)(2)(3)(4)(5)的运行顺序和运行结果,并说出原因
解决方案 »
- struts action跳转后加载flash的问题
- Strut2 性能,速度大讨论
- 在hibernate读取对象时遇到问题 java.lang.reflect.InvocationTargetException
- 如何固定若干PC, 除固定的PC 外,不能 Login, 提高安全性
- hibernate懒加载问题
- 连接池问题
- JBX+WEBLOGIC8.1+MSSQL整合问题
- 跪求一J2EE/WEBLOGIC问题答案
- 关于sun公司提供的J2EE1.4的安装和启动问题。。。。。。。。。。。。。。。
- 如何将long 类型的值转换成string类型
- axis的AdminClient的问题
- 关于JFreeChart的CPU和动态刷新曲线
2. Singleton obj = 写在 counter2 之前和之后有没有差别?
(2)public static int counter1; 初始默认为0
(3)public static int counter2 = 1;(1)(2)(3)程序初始的时候就对应赋值
(3)实例化了。。调用(4)private无参构造函数
所以counter1 = 1; counter2 = 2
(5)只是返回一个Singleton地址