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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:08:28+00:00 2026-05-26T01:08:28+00:00

As I was developing something, I wondered what might be the better/faster approach to

  • 0

As I was developing something, I wondered what might be the better/faster approach to create a bunch of objects while iterating over something.

Lets say we have this wrapper-class:

public class Photo{
    private String url;
    private String creator;

    public Photo(String url, String creator){
        this.url = url;
        this.creator = creator;
    }
    // Getter methods down here...
}

And we have a JSONArray of photos from which we want only the url– and the creator-strings:

JSONArray photos = json.getJSONArray("photos");
Photo[] photo_arr = new Photo[photos.length()];
// Iterate:
for (int i = 0; i < photos.length(); i++){
    // Create the objects here.
}

Now, I see three possible solutions:

Creating temporary variables

Creating some temporary variables which get the desired values from the current object and then construct the new Photo-object:

// Iterate:
String url = "";
String creator = "";
for (int i = 0; i < photos.length(); i++){
    url = photos[i].getString("url");
    creator = photos[i].getString("creator");
    photo_arr[i] = new Photo(url, creator);
}

Use the returned-values directly in the constructor

Don’t create temporary variables but use the returned values from the getString()-method in the constructor-call:

// Iterate:
for (int i = 0; i < photos.length(); i++){
    photo_arr[i] = new Photo(
        photos[i].getString("url"),
        photos[i].getString("creator")
    );
}

Using setter-methods

Adding a no-parameter constructor and setter-methods for the url and creator to the wrapper-class and use them to populate the object:

// Iterate:
for (int i = 0; i < photos.length(); i++){
    photo_arr[i] = new Photo();
    photo_arr[i].setUrl( photos[i].getString("url") );
    photo_arr[i].setCreator( photos[i].getString("creator") );
}

Which one is the better/faster/cleaner approach here?

  • 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-26T01:08:28+00:00Added an answer on May 26, 2026 at 1:08 am

    The two first methods are similar. Introduce variables if you feel it’s more readable and maintainable. The last one makes the Photo class mutable whereas it was immutable in the first two methods (not in the strictest meaning used in concurrency, though).

    You should always favor immutability when possible, mainly because it makes programs more robust: a Photo is always in a usable, fully constructed state in the first two cases. Not in the third one.

    Note that performance is irrelevant here: the three ways will certainly lead to comparable times, and it’s not that kind of code that will make a program slow. Don’t optimize prematurely. It’s the root of all evil.

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

Sidebar

Related Questions

I have just found something very weird while developing a website. While trying to
I am developing a MVC 3 Web app and I want to create something
I've been developing iPhone apps for a while now and something that's always peeved
I'm developing something for a bunch of users, and when I was testing it,
This is something that I don't see much discussed. I'm developing a software that
I'm developing an app using threading, and something weird is happening. I have these
I am developing WTL GUI application. I want to print something on console. How
I'm developing a REST service, so a request could be something like this: /Data/Main/Table=Customers/
We are developing something like a social networking website. I've got task to do
I'm asking for some orientation and recommendations for developing something similar like the yahoo

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.