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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:32:22+00:00 2026-05-13T19:32:22+00:00

class Foo(){ private String x,y; //getters setters } main(){ Foo bar1 = new Foo();

  • 0
class Foo(){
   private String x,y;
   //getters setters 
}
main(){
   Foo bar1 = new Foo();
   Foo bar2 = new Foo();
   bar1.setX("hey");
   bar2.setX("hi");
   bar2.setY(" there");
   setNewValuesFromLeftToRight(bar1,bar2);//left:bar1
   System.out.print(bar2.getX+bar2.getY)// hey there
}

setNewValuesFromLeftToRight : this method would get any 2 object with the same class and set field values of bar2 using field values,that are not null,of bar1
What is the best way to write the method setNewValuesFromLeftToRight ? sure it should be generic solution.
Will I use the Reflections?

  • 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-13T19:32:22+00:00Added an answer on May 13, 2026 at 7:32 pm

    The way I read these requirements that any property in the right (target) bean should be overwritten iff there is a corresponding non-null value in the left (source) bean. So that’s slightly different from PropertyUtils.copyProperties which would overwrite all properties (including null source values).

    One possibility would be to use Jakarta Commons BeanUtils, then you can use

    PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(leftBean);
    if (descriptors != null) {
      for (PropertyDescriptor descriptor : descriptors) {
        try {
          String propertyName = descriptor.getName();
          Object val = PropertyUtils.getProperty(leftBean, name);
          if (val != null) {
            PropertyUtils.setProperty(rightBean, name, val);
          }
        } catch (Exception ignore) {
          // not interested in what we can't read or write
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class like the one below: class Foo { private: std::map<std::string, Bar*>
I have a class: class foo { private: std::string data; public: foo &append(const char*
Which is more conventional in C#? class Foo { private string _first; private string
We have the following.. public class Foo { public string Name { get; private
class Foo { String bar; // private String bar; } public class A {
E.g. which way is better class Foo { private string _Bar ; Foo (
Consider this class: public class Foo { // Fields private string _bar; // Properties
public class foo { private String _name; private String _bar; public String getName() {
imagine this: public class Foo { private IList<string> WasSet; public string Prop1 {get;set;} public
Often I have a class as such: public class Foo { private String field1;

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.