在java中在哪里启动对象 [英] where to initiate object in java

查看:29
本文介绍了在java中在哪里启动对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与我对另一个问题的回答有关.原始问题是这里

This question relates to my answer of another of my question. The original question is here

任何人都可以用注释中解释的方式解释为什么坏代码会失败(因为它是伪代码)

Can anyone explain why the bad code fails in the way explained in the comments (by the wy it is in pseudo code)

 // bad code
        ResultSet rs = getDataFromDBMS("Select * from [tableName];");

            //temp uses a collection member within it to hold a list of column names to data value pairs (hashmap<String,String>)
        Object temp = new objectToHoldInfoFromResultSet();

        // loop over the result set
        while (rs.next)// for each row in the result set
        {
            for (int i = 1; i <= rs.getNumberColums; i++) {
                temp.add(infoAboutColumn);
            }
            temp.printInfo();// always prints correct (ie current) info
                    //the print just takes the hashmap member and asks for a       
                    //toString() on it
            anotherObject(makeUseOf(temp));// always uses info from first
                   //iteration. Essentially grabs the hashMap member of temp, and puts
                   //this into another collection of type 
                   //HashMap< HashMap<String,String>, temp> see the linked question
                   //for more detail.

        }

        // Seemingly each loop into the while the temp.doSomethingToData(); uses
        // the temp object created in the first iteration

        // good code
        ResultSet rs = getDataFromDBMS("Select * from [tableName];");

        // loop over the result set
        while (rs.next)// for each row in the result set
        {
            Object temp = new objectToHoldInfoFromResultSet();// moving
                                                                // declaration
                                                                // of temp into
                                                                // the while
                                                                // loop solves
                                                                // the problem.
            for (int i = 1; i <= rs.getNumberColums; i++) {
                temp.add(infoAboutColumn);
            }
            temp.printInfo();// always prints correct (ie current) info

            anotherObject(makeUseOf(temp));// also uses the correct (ie current)
                                            // info.

        }

推荐答案

如果不知道 temp.printInfo()makeUseOf() 是什么,我们就无法可靠地回答这个问题是做.不过,按照您描述的方式实施它们很容易.

We can't reliably answer this without knowing what temp.printInfo() and makeUseOf() are doing. It is easy to implement them to behave the way you describe though.

当您在循环外实例化 temp 时,您将在循环的所有迭代中使用相同的对象.因此,它可以从每次迭代中收集数据(例如,收集到一个集合中).然后,方法有可能获取当前迭代以及任何先前迭代中累积的数据,如果不是有意的,这可能会导致问题.

When you instantiate temp outside the loop, you will be using the same object throughout all iterations of the loop. Thus it is possible for it to gather data from each iteration (e.g. into a collection). And then it is possible for methods to get data accumulated in the current iteration, as well as from any previous iteration, which may cause problems if it was not intended.

所以让我们假设 temp 包含一个集合,并且在每次迭代中,结果集中的一列被添加到其中.现在,如果 temp.printInfo() 被实现来打印关于这个集合中 last 元素的信息,而 makeUseOf() 被实现来做一些事情使用集合中的 first 元素,您将获得所观察到的行为.

So let's assume temp contains a collection and in each iteration a column from the resultset is added to it. Now if temp.printInfo() is implemented to print info about the last element in this collection, while makeUseOf() is implemented to do something with the first element in the collection, you get the behaviour you observed.

OTOH 当你在循环内实例化 temp 时,你会在每次迭代中得到一个新的、不同的对象,它不会记住"来自早期迭代的任何数据.因此,即使使用上面概述的 temp.printInfo()makeUseOf() 的实现,您也会得到正确的结果.

OTOH when you instantiate temp inside the loop, you will get a new, distinct object in each iteration, which won't "remember" any data from earlier iterations. Thus even with the implementations of temp.printInfo() and makeUseOf() outlined above, you will get correct results.

这篇关于在java中在哪里启动对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆