楼主: MrZhang

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

[复制链接]
论坛徽章:
55
生肖徽章:虎
日期:2006-09-06 21:14:232011新春纪念徽章
日期:2011-01-25 15:41:502011新春纪念徽章
日期:2011-01-25 15:42:152011新春纪念徽章
日期:2011-01-25 15:42:332011新春纪念徽章
日期:2011-01-25 15:42:56管理团队成员
日期:2011-05-07 01:45:082012新春纪念徽章
日期:2012-01-04 11:49:542012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:18
31#
发表于 2005-6-21 13:09 | 只看该作者
最初由 pillow 发布
[B]静态变量的初始化在构造函数之后


[/B]


概念错误。static{}以及静态变量的初始化都是在ClassLoader加载类的时候执行的,而构造函数则是在类的实例化的时候执行的。所以静态变量的初始化应该在构造函数之前。

使用道具 举报

回复
论坛徽章:
2
沸羊羊
日期:2015-03-04 14:43:432015年新春福章
日期:2015-03-06 11:57:31
32#
发表于 2005-6-21 14:03 | 只看该作者

改成等价的代码看看 是不是觉得好理解了

public class SingletonTest
{

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

    private SingletonTest()
    {
        unStaticInt2 = 10;
        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 singletontest = getInstance();
        singletontest.printInt();
    }

}

------------------------------------------------------------------------------
public class SingletonTest
{

    private static SingletonTestsglnTest = null;
    private static int staticInt = 5;
    private int unStaticInt2;

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

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

    public void printInt()
    {
        System.out.println(staticInt + "----" + unStaticInt2);
    }

    public static void main(String args[])
    {
        SingletonTest singletest = getInstance();
        singletest.printInt();
    }

}

使用道具 举报

回复
论坛徽章:
38
ITPUB元老
日期:2005-04-12 14:04:392009新春纪念徽章
日期:2009-01-04 14:52:28生肖徽章2007版:马
日期:2009-01-24 22:03:20生肖徽章2007版:兔
日期:2009-03-10 21:20:36生肖徽章2007版:牛
日期:2009-04-10 12:57:06生肖徽章2007版:鸡
日期:2009-06-24 08:41:182009日食纪念
日期:2009-07-22 09:30:00生肖徽章2007版:虎
日期:2009-09-10 11:21:07生肖徽章2007版:猪
日期:2009-09-18 11:23:00IT宝贝
日期:2009-09-21 09:51:33
33#
发表于 2005-6-21 14:32 | 只看该作者
最初由 yining 发布
[B]

概念错误。static{}以及静态变量的初始化都是在ClassLoader加载类的时候执行的,而构造函数则是在类的实例化的时候执行的。所以静态变量的初始化应该在构造函数之前。 [/B]


Yining的语言掌握不错啊,还以为只会灌水呢

不过我不太编程序了
谁要是编上面容易那样造成歧义的程序,一定不让他通过。

使用道具 举报

回复
论坛徽章:
0
34#
发表于 2005-6-21 15:52 | 只看该作者
请问执行1) private static Singleton obj = new Singleton()时马上就执行4) private Singleton()
{
counter1++;
counter2++;
}
请问:这时候counter1和counter2还没初始化那,哪来的自加?

使用道具 举报

回复
论坛徽章:
0
35#
发表于 2006-9-28 16:02 | 只看该作者

请教

如果代码改成这样
package day1;
class Singleton
{
        private static Singleton obj = new Singleton();
        public static int counter1;
        public static int counter2 = 0;
       

        private Singleton()
        {
                counter1++;
                counter2++;
        }

        public static Singleton getInstance()
        {
                return obj;
        }

        public static void main(String[] args)
        {
                Singleton sing = Singleton.getInstance();
                System.out.println(sing.counter1);
                System.out.println(sing.counter2);
        }
}

只是把private static Singleton obj = new Singleton();
        public static int counter1;
        public static int counter2 = 0;
顺序变了,为什么结果又不同了???????

使用道具 举报

回复
论坛徽章:
1
36#
发表于 2006-9-29 09:20 | 只看该作者
感觉非常好,继续学习

使用道具 举报

回复
论坛徽章:
4
授权会员
日期:2006-09-30 14:30:13ITPUB元老
日期:2006-10-01 06:07:56会员2007贡献徽章
日期:2007-09-26 18:42:10ITPUB新首页上线纪念徽章
日期:2007-10-20 08:38:44
37#
发表于 2006-9-30 09:44 | 只看该作者
UP

使用道具 举报

回复
论坛徽章:
4
授权会员
日期:2006-09-30 14:30:13ITPUB元老
日期:2006-10-01 06:07:56会员2007贡献徽章
日期:2007-09-26 18:42:10ITPUB新首页上线纪念徽章
日期:2007-10-20 08:38:44
38#
发表于 2006-9-30 09:45 | 只看该作者
面试题目都是基础的.考基础能力

使用道具 举报

回复
论坛徽章:
0
39#
发表于 2008-4-7 20:32 | 只看该作者
lbdl谢谢

使用道具 举报

回复
论坛徽章:
0
40#
发表于 2009-4-25 09:32 | 只看该作者
有没有多一些这样的题目啊

使用道具 举报

回复

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

本版积分规则 发表回复

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