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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:55:29+00:00 2026-06-09T13:55:29+00:00

I have this question. But it will be difficult for me to explain as

  • 0

I have this question. But it will be difficult for me to explain as I don’t know exact terms to use. Hope someone will understand. I’ll try to discribe to the best i can. I feel like this is much related to parsing

Say there are two classes. And in both classes I have some variables, say strings (just for simplicity, variable type can be any), which have similar names.

Eg:
    class ClassA{
        String x,y,z;
    }

    class ClassB{
        String x,y,z;
    }

Now, what i need is, i need to copy the value of one class’s variable values to other classes corresponding variable.

Eg:
    ClassA aa=new ClassA();
    ClassB bb=new ClassB();
    //set bb's variables
    aa.x=bb.x;
    aa.y=bb.y;
    aa.z=bb.z;

like that.

But please note that what i need is not the above method. I hope there will be a way to write a simple method, so that it will identify the relevent variable by the name passed to it. Then it will do the value assignment accordingly.

My imagined method is like this,

void assign(String val){        
    // aa.<val>=val
}

For example if you pass bb.x to assign(...) method, then it will do aa.x=bb.x assignment.

Hope this is clear enough. There must be a better way to explain this. If someone know it please edit the post(+title) to make it more clear (But save my idea)..

Please let me know if there’s a way to achieve this.

Thanks!

  • 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-09T13:55:31+00:00Added an answer on June 9, 2026 at 1:55 pm

    Dozer is fine, see Jean-Remy’s answer.

    Also, if the variables have getters and setters according to the JavaBeans standard, there are a number of technologies that could help you, e.g. Apache Commons / BeanUtils

    Sample code (not tested):

    final Map<String, Object> aProps = BeanUtils.describe(a);
    final Map<String, Object> bProps = BeanUtils.describe(b);
    aProps.keySet().retainAll(bProps.keySet());
    for (Entry<String, Object> entry : aProps.entrySet()) {
        BeanUtils.setProperty(b,entry.getKey(), entry.getValue());
    }
    

    Update:

    If you don’t have getters and setters, here’s a quick hack that copies field values from one class to another as long as the fields have common names and types. I haven’t tested it, but it should be OK as a starting point:

    public final class Copier {
    
        public static void copy(final Object from, final Object to) {
            Map<String, Field> fromFields = analyze(from);
            Map<String, Field> toFields = analyze(to);
            fromFields.keySet().retainAll(toFields.keySet());
            for (Entry<String, Field> fromFieldEntry : fromFields.entrySet()) {
                final String name = fromFieldEntry.getKey();
                final Field sourceField = fromFieldEntry.getValue();
                final Field targetField = toFields.get(name);
                if (targetField.getType().isAssignableFrom(sourceField.getType())) {
                    sourceField.setAccessible(true);
                    if (Modifier.isFinal(targetField.getModifiers())) continue;
                    targetField.setAccessible(true);
                    try {
                        targetField.set(to, sourceField.get(from));
                    } catch (IllegalAccessException e) {
                        throw new IllegalStateException("Can't access field!");
                    }
                }
            }
        }
    
        private static Map<String, Field> analyze(Object object) {
            if (object == null) throw new NullPointerException();
    
            Map<String, Field> map = new TreeMap<String, Field>();
    
            Class<?> current = object.getClass();
            while (current != Object.class) {
                for (Field field : current.getDeclaredFields()) {
                    if (!Modifier.isStatic(field.getModifiers())) {
                        if (!map.containsKey(field.getName())) {
                            map.put(field.getName(), field);
                        }
                    }
                }
                current = current.getSuperclass();
            }
            return map;
        }
    }
    

    Call Syntax:

    Copier.copy(sourceObject, targetObject);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The question is a bit difficult to explain, but I will try. I have
I'm sorry to ask this question but I have spent hours trying to understand
This will be a bit difficult to explain but I will try my best.
I know this will be a difficult question, so I am not necessarily looking
Difficult to try and phrase this question, but I will try my best. Basically,
I have this old question but no answer works for me from online, the
I have read this question but it's not quite what I was looking for.
I have asked this question before but did not get the satisfied answer as
This question is kind of related to another question but I have a specific
(I have a problem that I illustrated in this question but had no correct

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.