楼主: MrZhang

[精华] 一个大公司JAVA考题的问题

[复制链接]
论坛徽章:
0
21#
发表于 2005-3-16 23:49 | 只看该作者
楼主说的这个叫做单态设计模式哈,是为了在设计中避免Singleton产生很多个对象.这里不管其他的类怎么调用,Singleton类只产生一个对象

使用道具 举报

回复
论坛徽章:
1
授权会员
日期:2005-10-30 17:05:33
22#
发表于 2005-3-17 10:30 | 只看该作者

Re: 纠正!

最初由 vidar520 发布
[B]

综述上面两个例子:

JAVA class中的初始化次序取决于变量在class中的定义次序,变量的定义可能散落各处,但变量一定会在任何一个函数(包括构造函数)被调用之前完成初始化。〕
  大家再有什么疑问可以参考Think in java中关于类成员初始化顺序的描述。 [/B]

=======================================

请看下面一个例子:
package pro_2;

public class SingletonTest {

    private static SingletonTest sglnTest = new SingletonTest();
    private static int staticInt = 5;
    private int unStaticInt2 = 10;

    private SingletonTest(){
        printInt();
        staticInt++;
        unStaticInt2++;
        printInt();
    }

    public static SingletonTest getInstance(){
        return sglnTest;
    }

    public void printInt(){
        System.out.println(staticInt+"----"+unStaticInt2);
    }
   
    public static void main(String[] args){
        SingletonTest single = SingletonTest.getInstance();
        single.printInt();
    }
}


-------------------------------------
输出结果为:
0----10
1----11
5----11

《thinking in java》 没怎么好好看,说说我自己的理解:假设要创建一个Test类。
虚拟机会首先创建一个Test.Class类型的对象,然后初始化其中的静态变量,之后再根据需要创建具体实例,这时确实是先初始化成员变量(非static)再调用构造函数创建一个具体实例然后返回一个引用。

但是对于单例模式(饿汉式)来讲是个特例,他在Singleton.Class初始化静态变量时已经调用了构造函数创建了一个实例。如果Singleton里有非static变量,创建实例时当然也是先初始化变量然后再调用构造函数,但是Singleton没有。
以后再使用Singleton的时候都不会再去创建新的实例而是直接调用getInstance()返回这个引用。
---------------------------------------
下面一段摘自<thinking in java-3e>

It’s helpful to summarize the process of creating an object. Consider a class called Dog:

The first time an object of type Dog is created (the constructor is actually a static method), or the first time a static method or static field of class Dog is accessed, the Java interpreter must locate Dog.class, which it does by searching through the classpath.
As Dog.class is loaded (creating a Class object, which you’ll learn about later), all of its static initializers are run. Thus, static initialization takes place only once, as the Class object is loaded for the first time.
When you create a new Dog( ), the construction process for a Dog object first allocates enough storage for a Dog object on the heap.
This storage is wiped to zero, automatically setting all the primitives in that Dog object to their default values (zero for numbers and the equivalent for boolean and char) and the references to null.
Any initializations that occur at the point of field definition are executed.
Constructors are executed. As you shall see in Chapter 6, this might actually involve a fair amount of activity, especially when inheritance is involved.

使用道具 举报

回复
论坛徽章:
1
授权会员
日期:2005-10-30 17:05:33
23#
发表于 2005-3-17 10:35 | 只看该作者
如果稍微做一下修改:
使用懒汉式单例模式,只在需要的时候才去创建实例如下:
package pro_2;

public class SingletonTest {

    private static SingletonTest sglnTest= null;
    private static int staticInt = 5;
    private int unStaticInt2 = 10;

    private SingletonTest(){
        printInt();
        staticInt++;
        unStaticInt2++;
        printInt();
    }

    public static SingletonTest getInstance(){
        if(sglnTest==null){
            sglnTest = new SingletonTest();
        }
        return sglnTest;
    }

    public void printInt(){
        System.out.println(staticInt+"----"+unStaticInt2);
    }
   
    public static void main(String[] args){
        SingletonTest single = SingletonTest.getInstance();
        single.printInt();
    }
}
---------------------------------------------
此时输出结果变为:
5----10
6----11
6----11

使用道具 举报

回复
论坛徽章:
0
24#
发表于 2005-3-17 16:17 | 只看该作者
不错,只是不喜欢他的编码风格

使用道具 举报

回复
论坛徽章:
0
25#
发表于 2005-3-17 22:39 | 只看该作者

补充。。。Re: Re: 纠正!

最初由 long_biti 发布
[B]
=======================================
下面一段摘自<thinking in java-3e>
It’s helpful to summarize the process of creating an object. Consider a class called Dog:

The first time an object of type Dog is created (the constructor is actually a static method), or the first time a static method or static field of class Dog is accessed, the Java interpreter must locate Dog.class, which it does by searching through the classpath.
As Dog.class is loaded (creating a Class object, which you’ll learn about later), all of its static initializers are run. Thus, static initialization takes place only once, as the Class object is loaded for the first time.
When you create a new Dog( ), the construction process for a Dog object first allocates enough storage for a Dog object on the heap.
This storage is wiped to zero, automatically setting all the primitives in that Dog object to their default values (zero for numbers and the equivalent for boolean and char) and the references to null.
Any initializations that occur at the point of field definition are executed.
Constructors are executed. As you shall see in Chapter 6, this might actually involve a fair amount of activity, especially when inheritance is involved. [/B]


Think in java(3e)说的很清楚:
1、Dog对象首次被产生出来,或者他的static 函数或者数据成员被访问,java interpreter去找出Dog.class
2、Dog.class被装载所有的初始化被执行(all of its static initializers are run)
3、new dog()在堆上分配空间
4、存储空间被清零,对象内的数据被赋予初值。(This storage is wiped to zero, automatically setting all the primitives in that Dog object to their default values (zero for numbers and the equivalent for boolean and char) and the references to null. )
5、执行所有发生在数据定义处的初始化动作。(Any initializations that occur at the point of field definition are executed. )
6、执行构造函数(Constructors are executed. )

使用道具 举报

回复
论坛徽章:
1
授权会员
日期:2005-10-30 17:05:33
26#
发表于 2005-3-18 09:33 | 只看该作者
楼上翻译的实在是很简练

使用道具 举报

回复
论坛徽章:
0
27#
发表于 2005-6-21 09:12 | 只看该作者

need help!

我是一个java初学者,请各位java前辈多指教!前两个程式看了一下,区别只在于SingletonTest sglnTest= null;这句代码,但两个程式的调用却不同,不单单因为变量作用域的问题吧。如果可以请给我发e-mail来告诉我。谢谢!
还有什么考题,答案请多多发给我,万分感谢!

使用道具 举报

回复
论坛徽章:
0
28#
发表于 2005-6-21 10:43 | 只看该作者
不错,很有收获

使用道具 举报

回复
论坛徽章:
0
29#
发表于 2005-6-21 11:24 | 只看该作者
如果执行顺序是1,4,2,3,5的话 那么4 的构造函数内的counter1和counter2的值并不知道啊??

使用道具 举报

回复
论坛徽章:
0
30#
发表于 2005-6-21 12:43 | 只看该作者
最初由 lbdl 发布
[B]其实不是执行2次,第一次是初始化,分配内存而已。

顺序是:1-4-3-5(静态存储区初始化)

2不执行。因为第二步没有进行变量初始化。 [/B]


如果执行顺序是1,4,3,5的话 那么4 的构造函数内的counter1和counter2的值并不知道啊??

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表