简体   繁体   中英

java -pojo Object instantiation outside of loop or inside?

I have some code with a loop and a basic pojo instantiation. Int he loop pojo gets added to the list after its attributes get set via use of setters. My question is, what is better and why regarding instantiating the pojo inside of the loop and use the pojo's setter methods inside of loop or instantiate the pojo outside the loop and use the pojo's setters inside the loop to be specific. I am referring to the pojo variable in the code below. HumanCoverageData hcd = new HumanCoverageData();

    HumanCoverage hc = new HumanCoverage();
    List<HumanCoverageData> HumanCoverageDataList = new ArrayList<HumanCoverageData>();
    // Coverage is a static nested class not shown in the code.
    for (Coverage c : sec.getCoverage()){

        HumanCoverageData hcd = new HumanCoverageData();
         hcd.setFirstName(c.getUser_name().split(",")[0]); 
         hcd.setLastName(c.getUser_name().split(",")[1]); 
         hcd.setPhoneNumber(c.getPhone_number()); 
         hcd.setRoleCode(c.getRolecode()); 
         hcd.setRoleDescription(null); 
         hcd.setUserId(c.getUser_id()); 

         HumanCoverageDataList.add(hcd);
    }

    hc.setHumanCoverageList(HumanCoverageDataList);
    co.setCoverage(hc);

IMHO, you must instantiate the POJO inside the loop because you use different objects for every step and then you add in your list. If you instantiate you POJO outside, you must care to memory references

It seems to me like each HumanCoverageData depends on a single Coverage object. It therefore makes no sense to instantiate HumanCoverageData outside of it.

You would end up re-initializing it anyway unless you wanted to find yourself with N times the same reference in your list

HumanCoverageData hcd = new HumanCoverageData();
for (Coverage c : sec.getCoverage()){

     hcd.setFirstName(c.getUser_name().split(",")[0]); 
     hcd.setLastName(c.getUser_name().split(",")[1]); 
     hcd.setPhoneNumber(c.getPhone_number()); 
     hcd.setRoleCode(c.getRolecode()); 
     hcd.setRoleDescription(null); 
     hcd.setUserId(c.getUser_id()); 

     HumanCoverageDataList.add(hcd);
}

Your HumanCoverageDataList would contain sec.getConverage().size() times the same object/same reference. This is because you called new only once. When the for loops and the setters are executed, all you are doing is overwriting the previous value.

Instead you could declare hcd outside of the for loop if you wanted to use it after the loop, ie. outside the scope of the loop

HumanCoverageData hcd = null;
for (Coverage c : sec.getCoverage()){
     hcd = new HumanCoverageData();
     hcd.setFirstName(c.getUser_name().split(",")[0]); 
     hcd.setLastName(c.getUser_name().split(",")[1]); 
     hcd.setPhoneNumber(c.getPhone_number()); 
     hcd.setRoleCode(c.getRolecode()); 
     hcd.setRoleDescription(null); 
     hcd.setUserId(c.getUser_id()); 

     HumanCoverageDataList.add(hcd);
}

Read up on variable scope here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM