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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:34:23+00:00 2026-06-15T12:34:23+00:00

I have several situations where a class consists of an arbitrary (but fixed) number

  • 0

I have several situations where a class consists of an arbitrary (but fixed) number of variables of differing types and wish to iterate over those variables as well as use their names in code. Is there a smarter way to do this than to retype each variable name in each function?

My initial thought is to use a HashMap to store the local variables but that seems inefficient and doesn’t handle multiple types.

Code below is simulated to shorten the required reading, but typically its more than 3 variables:

class Widget implements Parcelable {
    String name, code;
    Long price;

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(code);
        dest.writeLong(price);
    }

    public Widget(Parcel o) {
        name = o.name;
        code = o.code;
        price = o.price;
    }

    public String getXML() {
        return "<Widget><name>"+name+"</name><code>"+code
              +"</code><price>"+price+"</price></Widget>";
    }
}

Instead, I’d prefer to do something like (pseudo-code):

public Widget(Parcel o) {
    for (Map<Name,Data> item: o.getMap()) {
        this.setValue(Name, Data);
    }
}
public String getXML() {
    StringBuilder XML = new StringBuilder();
    XML.append("<Widget>");
    for (Map<Name,Data> item: o.getMap()) {
        XML.append("<"+Name+">" + Data + "</"+Name+">");
    }
    XML.append("</Widget>");
    return XML.toString();
}

I keep thinking there must be a pattern for doing this type of thing, but maybe its just my Python history kicking at my Java experience.

Update: this is not about solely fetching XML from member variables, but about looping through member variables in multiple member functions. It would appear that Java’s reflection features were missing from my repertoire.

My new smaller constructor makes me glad to have learned something useful, and looks like:

public Widget(Widget other) {
    for (Field f : getClass().getDeclaredFields()) {
        if (f.getName() == "TAG") {
            continue;
        }
        f.set(this, f.get(other));
    }
}
  • 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-15T12:34:24+00:00Added an answer on June 15, 2026 at 12:34 pm

    That is usually solved with reflection:

    for (Field f : getClass().getDeclaredFields()) {
        String name = f.getName();
        String value = f.get(this);
    }
    

    However, for writing to / reading from XML, you probably want to use JAXB rather than reinventing the wheel.

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

Sidebar

Related Questions

I have had several situations when i would like to do that. This could
Background: I have a ListView / GridView with several columns. In some situations, only
I have a situation I've run into several times but have never found a
I have looked at several possible solutions to my issue but did not find
I have a situation where we have a base class which several subclasses implement.
I have a parent class with several children. One of the children, has one
I have an app which consists of several different assemblies, one of which holds
I need to selectively run several functions depending on various situations. I have a
I have a situation where I have several VB.NET Modules in the same Logical-Module
So the situation is like this: I have several links, each with one hidden

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.