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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:38:34+00:00 2026-05-13T09:38:34+00:00

How can I go about populating a simple Java object with data from any

  • 0

How can I go about populating a simple Java object with data from any other arbitrary object?

For example, I can end up with a Document object whose children nodes I can iterate through and I want to set same-named properties in another object with the node’s values.

I work primarily in dynamic languages and I think I am hung up on how this would work in perl or javascript and can’t get my head out of the dynamic gutter long enough to see this clearly.

I know I could do something like (pseudo code)

while (key = nextKey) {
    if (key.name == "fooBar") {
        object.setFooBar(key.value);
    } else if (key.name == "bazQux") {
        object.setBazQux(key.value);
    }
    ...etc...
}

But that just doesn’t feel good, and feels awful when the number of properties or complexity increases.

In a dynamic language, i would do something like:

while (key = nextKey) {
    object.setField(key.name, key.value);
    // or even
    object.[key.name] = key.value;
}

where setField could be a dispatch table with code references. I know I don’t have the luxury of every object being a hash by default, but I’m looking for general advice. How would you do it?

A switch/case would be a little better, but java complains about it not enjoying Strings in such statements. Would enums be a solution? </blind stabbing for answers>

I’ve looked into using reflection in order to implement some sort of automatic dispatch table, but that strikes me as being looked down upon and that there has to be a better way.

Thanks for any insight.

  • 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-13T09:38:34+00:00Added an answer on May 13, 2026 at 9:38 am

    Another vote for reflection. What you’re trying to do here is automatically bind untyped String data to typed Java variables. That’s simply going to require reflection.

    The reason reflection is “looked down upon” is because generally speaking, blindly binding untyped String data to typed Java variables is “looked down upon”. In many cases it’s better to (for example) have your object explicitly pull out only the data it needs. For example:

    this.fooBar = doc.get("fooBar");
    this.bazQux = doc.get("bazQux");
    // ... etc.
    

    You might say, “But now if I add a sysBiz entry to my document, I have to remember to add an extra line to my constructor. In a dynamic language it would Just Work[tm]!” That’s true, but in Java you have to add your sysBiz member variable, your getters and setters, and all the other logic around sysBiz anyway, so an extra line in your constructor or deserialization section isn’t all that much of a nuisance. Plus, it makes explicit which parts of the document you care about, so if somebody adds a zigBlog entry to the document, it won’t cause problems with your automatic binding (e.g. something that looked like object.setField(key, val)) throwing a runtime error about no zigBlog field existing.

    All that said, there are times when automatically binding untyped String data to typed Java variables is the right way to go, and reflection is the right tool to use in that scenario. Make sure that automatic binding really is the best choice for the data structures you’ve got, but once you’re sure, there’s no need to hesitate to use reflection just because it’s “looked down upon” or is a slightly more advanced section of the language. That’s exactly what it’s there for.

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

Sidebar

Ask A Question

Stats

  • Questions 291k
  • Answers 291k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Ok first of all .. You mention 10 minutes and… May 13, 2026 at 5:56 pm
  • Editorial Team
    Editorial Team added an answer You can change the http session-timeout in $ALBPM_HOME/webapps/portal/WEB-INF/web.xml: <session-config> <session-timeout>20</session-timeout>… May 13, 2026 at 5:56 pm
  • Editorial Team
    Editorial Team added an answer It's probably because you're running the page locally. IE doesn't… May 13, 2026 at 5:56 pm

Related Questions

I am currently converting what was a Console Application into a Windows Form application.
Preamble: I am using ASP.NET 3.5 and AJAXControlToolkit. I am trying to use the
I've got an ms-access application that's accessing and ms-sql db through an ODBC connection.
class EditAdminForm(forms.ModelForm): password = username.CharField(widget=forms.TextInput()) password = forms.CharField(widget=forms.PasswordInput()) password_confirm = forms.CharField(widget=forms.PasswordInput(), initial=???) You can
How can I go about generating a Friendly URL in C#? Currently I simple

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.