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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:19:11+00:00 2026-06-17T09:19:11+00:00

Im getting class name (string) and the class have few sets method and since

  • 0

Im getting class name (string) and the class have few sets method and
since Its dynamic (I can get any class) I need to use all the sets method and update it with data .
how can I do that ?

To get the class field I’m using the following code

className = obj.getClassName();
Class<?> classHandle = Class.forName(className);

for example here I need to update firstName and last name

public class Person {

private String id;
    private String firstName;
    private String lastName;

    public void setLastName(String lastName) {

        this.lastName = lastName;
    }

    public void setfirstName(String firstName) {

        this.firstName = firstName;
    }

or different class here i need to set the salary and the job description

public class Job {


  private double salery;
  private String jobDescr;


  public void setSalery(double salery) {
    this.salery = salery;
  }

  public void setJobDescr(String jobDescr) {
    this.jobDescr = jobDescr;
  }

}
  • 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-17T09:19:11+00:00Added an answer on June 17, 2026 at 9:19 am

    For starters, What you’ve done is good. I assume you have a Map<String, Object> of the attributes to be set: attributeMap.

    //this is OK
    className = obj.getClassName();
    Class<?> classHandle = Class.forName(className);
    
    //got the class, create an instance - no-args constructor needed!
    Object myObject = classHandle.newInstance();
    
    //iterate through all the methods declared by the class  
    for(Method method : classHandle.getMethods()) {
       //check method name
       if(method.getName().matches("set[A-Z].*") 
           //check if it awaits for exactly one parameter
           && method.getParameterTypes().length==1) {
    
           String attributeName = getAttributeName(method.getName());
           //getAttributeName would chop the "set", and lowercase the first char of the name of the method (left out for clarity)
    
           //To be extra nice, type checks could be inserted here...
           method.invoke(myObject, attributeMap.get(attributeName));            
    
       }
    }
    

    Of course, a lot of exception handling is to be done, this is just a basic idea of what is to be done…

    Recommended reading:

    • Class
    • Class.getMethods()
    • Method
    • Method.invoke()
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three domain objects defined as : class Member { String name static
I have a composite component: <composite:interface> <composite:attribute name=actionMethod method-signature=java.lang.String action() required=true /> </composite:interface> <composite:implementation>
I have a class like this : class User { String name; String password;
I have a problem with getting a class from an external .jar file. I
From some library, I'm getting such an input string: <link> <name>button1</name> <target>there</target> </link> <link>
I'm trying to figure out why I keep getting a few trying to get
I have a class containing Map whose key is string and value is object
if I have a string in a class, then memory is allocated. Do I
I have created a program for my ASP.NET class to create a class name
I have a static class which only contains string properties. I want to convert

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.