Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6190655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:37:38+00:00 2026-05-24T02:37:38+00:00

When using an object repeatedly, is it better to clear the data by setting

  • 0

When using an object repeatedly, is it better to clear the data by setting it to null, or instantiate a new object?

   Object a = new Object();
   for(...){
       ...
       a = null;
       **OR**
       a = new Object();
    }

Here is the example I’m referring to:

Customer a = new Customer();
Collection<Customer> data = new ArrayList<Customer>();

        while (rs != null && rs.next()) {   
            a = new Customer();
            a.setCustId(rs.getLong("CUST_ID"));
            a.setPerNo(period);
            a.setName(rs.getString("cust_nm"));

            if(a!= null)
                data.add(a);
        }

I’m wondering whether the

a = new Customer();

is the best way to do this or if it should be done differently to save on memory and for optimum performance, because each loop has new customer information to put in. From my understanding, if you create a new customer you’re creating a new object and pointing a to that new object. So the old object a was pointing to will get picked up by the garbage collector – same in the case of setting it to null. Is this a correct understanding?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:37:39+00:00Added an answer on May 24, 2026 at 2:37 am

    I would prefer:

    for (...) {
        Object a = new Object();
        ...
    }
    

    Why make the scope any greater than it needs to be?

    Likewise, unless I needed a new object, I wouldn’t create one for the sake of it.

    It’s hard to say much more from just the description given though – if you can give a more complete and real-world example, we may be able to make more concrete recommendations.

    (Note that there’s no indication that you want to use an object repeatedly, so much as using a variable repeatedly. They’re very different concepts.)

    EDIT: Looking at your specific example, I’d write it like this:

    Collection<Customer> data = new ArrayList<Customer>();
    
    while (rs != null && rs.next()) {   
        Customer a = new Customer();
        a.setCustId(rs.getLong("CUST_ID"));
        a.setPerNo(period);
        a.setName(rs.getString("cust_nm"));
    
        data.add(a);
    }
    

    Note that this doesn’t create any Customer objects which are eligible for garbage collection – whereas your original code creates an instance of Customer before entering the loop, and then ignores the newly created object, instead creating a new one and reassigning a‘s value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Oracle object data types to represent a timespan or period. And I've
In a project our team is using object lists to perform mass operations on
I am trying to access member variables of a class without using object. please
What is the difference between the following two snippets of code: using (Object o
I am building a web application and have been told that using object oriented
Using SSMS Object Explorer to click through explorer tree against a database with many
Does creating an object using reflection rather than calling the class constructor result in
I am using HttpContext object implemented in HttpHandler child to download a file, when
I'm interested in using an Object-Relational Mapping package for an upcoming project. This project
I'm creating a color object using the following code. curView.backgroundColor = [[UIColor alloc] initWithHue:229

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.