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 6331023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:58:48+00:00 2026-05-24T17:58:48+00:00

for example Aclass myAclass = new Aclass(); for(int i=0;i<n;i++) { myAclass.load(i); myAclass.do(); myAclass.clear() //

  • 0

for example

  Aclass myAclass = new Aclass();
  for(int i=0;i<n;i++)
  {
     myAclass.load(i);
     myAclass.do();
     myAclass.clear()  // clear the state of myAclass, rdy for next iteration.
  }

or what if I embed the `myAclass.load() into the class constructor, and do sth like:

   for(int i=0;i<n;i++)
   {
      Aclass myAclass = new Aclass(i);
      myAclass.do();
   }

So which way is better practice?
btw, the title seems not fitting the content, help me with a more appropriate title.

Note: Constructor of Aclass is trivial, Load() is not, Clear() is trivial
(of course the constructor in the 2nd example is not trivial as it triggers Load().)

  • 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-24T17:58:49+00:00Added an answer on May 24, 2026 at 5:58 pm

    There is no way to tell which approach is best in terms of performance, it depends on the implementation of the class.

    Now, as a general rule, you should avoid mutable objects, so reusing the same instance of the class to do something else is usually not a good idea. So I would favor the second approach. Of course, if the initialization of the class is an expensive operation, the first approach will be faster…

    As for memory usage, the second approach will create more instances in memory, but they will eventually be collected by the GC, so it shouldn’t be an issue. The only drawback is that it puts more pressure on the GC, so it could negatively affect performance if there are many iterations.

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

Sidebar

Related Questions

If I have for example a class with instance method and variables class Foo
For example i got a class and its got its own properties and i
What is a simple example of decorating a class by defining the decorator as
Can a class return an object of itself. In my example I have a
For example, I have a class with a public field and corresponding getter/setters: public
I'd like to have a class A with a (for example) SortedList collection SrtdLst
Say I have a class ObjectA (a view controller for example), that does this
Take the following example, I have a class public class SomeItem { public string
The fictional example here. Suppose I designed a class named IODevice. This class already
What is a class level lock. Can you please explain with an example.

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.