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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:17:33+00:00 2026-06-09T21:17:33+00:00

I have the following problem and wonder whether there is an efficient solution to

  • 0

I have the following problem and wonder whether there is an efficient solution to it.
(I am using Java)

Imagine you have multiple different types of classes holding the same data in variables with different name and consider this given.

Here an example:
Imagine there are the three empiric values as member within a container class

  1. short color
  2. int size
  3. String shape

and consider the two classes

  1. class1
  2. class2

class1 has three member variables being the empiric values:

  1. short rgb_color -> corresponds to color
  2. long bigness -> corresponds to bigness
  3. String contour -> corresponds to shape

class2 has three member variables being the empiric values:

  • int cmyk -> corresponds to color
  • int greatness -> corresponds to bigness
  • String shapecountour -> corresponds to shape

As you see the names are different. So if I want to import the values from class one and two into the container class, I would need to
convert every parameter by itself in order to add it to the container class and thus
I need to type as there are member variables (here 6)
e.g. see this pseudo code for the import function:

public void import(class1 class){
   this.color = (short) class.rgb_color;
   this.size = (int) class.bigness;
   this.shape = (String) class.contour;
}
public void import(class2 class){
   this.color = (short) class.cmyk;
   this.size = (int) class.greatness;
   this.shape = (String) class.shapecontour;
}

Now imagine problems, where there are much more parameters.
Is there a generic way to solve the import as to do it one by one for each member?

Thank you for your help.

EDIT: Thanks already for the fast answers.
As I said I cannot modify class1 and class2.

I have checked the reflection, where they have this example for changing the fields.

public class Book {
    public long chapters = 0;
    public String[] characters = { "Alice", "White Rabbit" };
    public Tweedle twin = Tweedle.DEE;

    public static void main(String... args) {
    Book book = new Book();
    String fmt = "%6S:  %-12s = %s%n";

    try {
        Class<?> c = book.getClass();

        Field chap = c.getDeclaredField("chapters");
        out.format(fmt, "before", "chapters", book.chapters);
        chap.setLong(book, 12);
        out.format(fmt, "after", "chapters", chap.getLong(book));

        Field chars = c.getDeclaredField("characters");
        out.format(fmt, "before", "characters",
               Arrays.asList(book.characters));
        String[] newChars = { "Queen", "King" };
        chars.set(book, newChars);
        out.format(fmt, "after", "characters",
               Arrays.asList(book.characters));

        Field t = c.getDeclaredField("twin");
        out.format(fmt, "before", "twin", book.twin);
        t.set(book, Tweedle.DUM);
        out.format(fmt, "after", "twin", t.get(book));

        // production code should handle these exceptions more gracefully
    } catch (NoSuchFieldException x) {
        x.printStackTrace();
    } catch (IllegalAccessException x) {
        x.printStackTrace();
    }
    }
}

But still I need to call each variable by name as e.g. “chapters”.
What do I get wrong?

  • 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-09T21:17:35+00:00Added an answer on June 9, 2026 at 9:17 pm

    You have two options to resolve the issue:

    1. To change the design, as suggested above. It seems that inheritance of some base class (with these three fields) should work fine.
    2. If you don’t have a way to modify these two classes (class1, class2) => You have to specify the mapping between the fields anyway. The straightforward way is your option with converters. Other option is to define the mapping with annotations/xml/etc. and to use some mapping tool. These conversation has a good list of them:

    any tool for java object to object mapping?

    Dozer seems to be the most promising for me.

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

Sidebar

Related Questions

I am using GWT/JAVA for development. I have following problem: I want to remove
I have the following problem. I have embedded a map using Google Maps' API.
I have the following problem, from c++ I send huge string[] to java. huge
i wonder how i can solve the following problem. i have a horizontal scrollbar
I have multiple pages with java applet written with SWT. The problem is, applet
I wonder whether someone can help me please. I have the following xml file
I'm using Delphi 2007 and I wonder how the following problem can be solved:
I have the following problem: there is a class with a couple of string
I have following problem: I have to make a ASP.NET Webapplication with a two-row
I have following problem: I have built a tabbar application with 4 tabs. I

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.