深入JVM之号称世界上所有Java程序员都会犯的一个错误
2012-03-10 15:15
477 查看
package com.jadyer.classloader; /** * 深入JVM之号称世界上所有Java程序员都会犯的一个错误 * @author 宏宇 * @editor Jan 24, 2012 7:49:18 PM * @see 这是一个很无耻的面试题,多么卑鄙的人才能写出这种自己给自己找麻烦的代码啊~~ */ public class ClassLoadTest { public static void main(String[] args) { SingletonFront singletonFront = SingletonFront.getInstance(); System.out.println("counter11 = " + singletonFront.counter11); System.out.println("counter22 = " + singletonFront.counter22); System.out.println("============="); SingletonBack singletonBack = SingletonBack.getInstance(); System.out.println("counter33 = " + singletonBack.counter33); System.out.println("counter44 = " + singletonBack.counter44); } } /** * 单例类:在变量之前new的实例 * @see Step01:为静态变量分配内存并初始化为默认值 * @see singletonFront=null,counter11=0,counter22=0 * @see Step02:为静态变量赋正确的初始值,并初始化类的实例 * @see singletonFront=new SingletonFront(),counter11=1,counter22先因为构造方法等于壹之后又因为初始值等于零 */ class SingletonFront{ private static SingletonFront singletonFront = new SingletonFront(); //注意这个位置 public static int counter11; public static int counter22 = 0; private SingletonFront(){ counter11++; counter22++; } public static SingletonFront getInstance(){ return singletonFront; } } /** * 单例类:在变量之后new的实例 * @see Step01:为静态变量分配内存并初始化为默认值 * @see counter11=0,counter22=0,singletonFront=null * @see Step02:为静态变量赋正确的初始值,并初始化类的实例 * @see counter11与counter22都是先等于零之后又因为构造方法才等于壹,singletonFront=new SingletonFront() */ class SingletonBack{ public static int counter33; public static int counter44 = 0; private static SingletonBack singletonBack = new SingletonBack(); //注意这个位置 private SingletonBack(){ counter33++; counter44++; } public static SingletonBack getInstance(){ return singletonBack; } }
相关文章推荐
- 号称是世界上所有的Java程序员都会犯的错误
- 论全世界所有程序员都会犯的错误-Java基础-Java-编程开发
- 转:JVM的运行原理 一个号称打败所有程序员的一道题目
- 全世界所有程序员都会犯的错误及自己的一点补充说明
- 全世界所有程序员都会犯的错误zz
- 自己收集的,全世界所有程序员都会犯的错误
- Java程序员惯性思维的一个错误
- 当世界上只剩下一个Java程序员
- 【深入Java开发】JVM源码分析之一个Java进程究竟能创建多少线程
- 一个多数程序员都会犯的错:Java方法传参的问题
- 当世界上只剩下一个Java程序员
- Java程序员惯性思维的一个错误
- 号称所有程序员都会犯错的程序
- 一个JVM的错误让我遇到了:A fatal error has been detected by the Java Runtime Environment
- [引用]全世界所有程序员都会犯的错误
- 全世界所有程序员都会犯的错误(转贴)
- 全世界所有程序员都会犯的错误-蔡学镛
- 蔡学镛专栏:全世界所有程序员都会犯的错误
- 全世界所有程序员都会犯的错误-蔡学镛
- 所有的苦难都会过去-----一个程序员的失业史