Here is the code:

 /*
Some class,such as a config file,need to be only one.So we need to control the instance.
1,private the constructor and create only one instance in the class itself.
2,provide a method for the others to get the 'only one' instance.
*/
package kju.obj; import static kju.print.Printer.*;
public class SingletonDemo {
public static void main(String[] args) {
SingleConfig con01 = SingleConfig.getInstance();
SingleConfig con02 = SingleConfig.getInstance();
println("con01 color : " + con01.getColor());
println("con02 set color : ");
con02.setColor("Blue");
println("con01 color : " + con01.getColor());
/*
con01 color : Orange
con02 set color :
con01 color : Blue
*/
}
} class SingleConfig {
private String color = "Orange";
private static SingleConfig s = new SingleConfig(); private SingleConfig() {}
public static SingleConfig getInstance() {
return s;
} public void setColor(String color) {
this.color = color;
} public String getColor() {
return color;
}
}

And the corresponds the code above:

最新文章

  1. SpringMVC无法获取请求中的参数的问题的调查与解决(2)
  2. Chrome开发者工具详解(4)-Profiles面板
  3. jsp中frameset frame不显示页面
  4. 151008:javascript不明白的地方
  5. Force.com微信开发系列(七)OAuth2.0网页授权
  6. 【Unity3D】Unity3D之 注册表动态存取游戏存档——PlayerPrefs类
  7. 检测网络变化(wifi、2g、3g、4g)
  8. [kmp+dp] hdu 4628 Pieces
  9. 去掉谷歌input记住账号或密码时默认出现的黄色背景
  10. asp.net web api 构建api帮助文档
  11. 前端笔记知识点整合之JavaScript(二)关于运算符&初识条件判断语句
  12. C#与SQL Server数据库连接
  13. Activity与Fragment数据传递之Fragment从Activity获取数据
  14. 1.rabbitmq高可用方案
  15. 多任务Forth系统内存布局
  16. 1552/3506. [CQOI2014]排序机械臂【平衡树-splay】
  17. linux缓存手动清理
  18. CentOS7.2 安装nginx-1.10.3
  19. PAT 甲级 1050 String Subtraction
  20. java 接口转码、加密

热门文章

  1. JPA2.1 中三个提升应用性能的新功能
  2. 如何在DJANGO里,向有外键(一对多和多对多)的DB里插入数据?
  3. 【POJ2699】The Maximum Number of Strong Kings(网络流)
  4. Javascript禁止网页复制粘贴效果,或者复制时自动添加来源信息
  5. QT 读写二进制 (数值)高位在前
  6. HTTP之Content-Length
  7. POI做题记录:第二届POI
  8. codeforces --- 115A
  9. git入门超详细(转载)
  10. ipython notebook使用教程