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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:11:50+00:00 2026-06-13T15:11:50+00:00

In javascript, I can do this: function MyObject(obj) { for (var property in obj)

  • 0

In javascript, I can do this:

function MyObject(obj) {
    for (var property in obj) {
        this[property] = obj[property];
    }
}

Can I do anything close in Java?

class MyObject {
    String myProperty;

    public MyObject(HashMap<String, String> props) {
        // for each key in props where the key is also the name of
        // a property in MyObject, can I assign the value to this.[key]?
    }
}
  • 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-13T15:11:51+00:00Added an answer on June 13, 2026 at 3:11 pm

    Yes, you can do it by reflection with something along the following lines:

    /**
     * Returns a list of all Fields in this object, including inherited fields.
     */
    private List<Field> getFields() {
        List<Field> list = new ArrayList<Field>();
        getFields(list, getClass());
        return list;
    }
    
    /**
     * Adds the fields of the provided class to the List of Fields. 
     * Recursively adds Fields also from super classes.
     */
    private List<Field> getFields(List<Field> list, Class<?> startClass) {
        for (Field field : startClass.getDeclaredFields()) {
            list.add(field);
        }
        Class<?> superClass = startClass.getSuperclass();
        if(!superClass.equals(Object.class)) {
            getFields(list, superClass);
        }
    }
    
    public void setParameters(Map<String, String> props) throws IllegalArgumentException, IllegalAccessException {
        for(Field field : getFields()) {
            if (props.containsKey(field.getName())) {
                boolean prevAccessible = field.isAccessible();
                if (!prevAccessible) {
                    /*
                     * You're not allowed to modify this field. 
                     * So first, you modify it to make it modifiable.
                     */
                    field.setAccessible(true);
                }
                field.set(this, props.get(field.getName()));
    
                /* Restore the mess you made */
                field.setAccessible(prevAccessible);
            }
        }
    }
    

    However, if you are not very familiar with Java, this approach should be avoided if at all possible, as it is somewhat dangerous and error prone. For instance, there is no guarantee that the Field you are attempting to set are actually expecting a String. If it is the case that they are not, your program will crash and burn.

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

Sidebar

Related Questions

Can somebody explain to me what this does in javascript? (function (x,y){}(x,y)); or this
I can bind a jquery event to this element like: <script type=text/javascript> $('#new_key').ready(function() {
can someone explain me why this script is not working? <script type=text/javascript> function destroy(ID)
You can backreference like this in JavaScript: var str = 123 $test 123; str
I have an object in Javascript that looks like this function MyObject(){ this.name=; this.id=0;
I've simulated a static class variable in Javascript. MyObject.staticVariable = hello world; function MyObject()
Does anyone recognize this JavaScript function? function (E,F){ var D=F||window; var A=[]; for(var C=0,B=this.length;C<B;++C){
I have a javascript object as such (simplified). var MyObject = (function(){ var propertyOne
I am trying to create a JavaScript object as follows. var MyObject = function
I'm very new to Javascript and somewhere i found this code var myObject =

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.