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

  • SEARCH
  • Home
  • 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 8178193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:42:14+00:00 2026-06-06T23:42:14+00:00

I have some doubts/questions regarding the object creation. I have heard that objects should

  • 0

I have some doubts/questions regarding the object creation. I have heard that
objects should not be created in the loop. Whats wrong with the creation of
objects inside the loop? Whats the difference between creating outside the loop
and creating inside the loop?

Please consider the following example.

public java.util.List<Object> objectCreationTest(){
    java.util.List<Object> objectList =new java.util. ArrayList<Object>();
    Object obj = null;
    for(int i = 0 ; i <1000;i++){
        Object e = new Object(); //1 -->  Is this object creation wrong? 
        obj = new Object(); //2 --> Is this right way to create?
        objectList.add(e );

    }

    return objectList ;

}

Please suggest me which way I have to follow?

  • 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-06-06T23:42:15+00:00Added an answer on June 6, 2026 at 11:42 pm

    I have heard that objects should not be created in the loop. Whats
    wrong with the creation of objects inside the loop?

    There’s nothing wrong with creating objects in a loop. Sometimes it’s the only way to create a number of (related) objects.

    In your example, you can simply do:

    public java.util.List<Object> objectCreationTest(){
        java.util.List<Object> objectList = new java.util. ArrayList<Object>();
        for(int i = 0 ; i <1000;i++){
            Object e = new Object(); 
            objectList.add(e);    
        }
        return objectList;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some simple doubts about NSOperation and GCD that I have not found
This Question arises from a Q&A here I have some doubts that i think
I have a couple of questions and doubts to regarding PowerShell and .NET Classes.
I have this specific case that bring me some doubts: My class A have
There have been some questions on SO recently on ADL that have got me
I have some doubts regarding the following controller action (in ASP.NET MVC, but it's
I have seen this question, and have some more doubts regarding creating a jar
I have some doubts regarding concurrency of posix threads in multiprocessor machine. I have
I have some doubts. What is querystrng? how we can use it? what are
I have some doubts about construction and initialization order guarantees in C++. For instance,

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.