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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:19:42+00:00 2026-05-17T18:19:42+00:00

I have a class that has many settable/gettable attributes. I’d like to use reflection

  • 0

I have a class that has many settable/gettable attributes. I’d like to use reflection to set these attributes, but I have 2 questions about my implementation

Here is some stripped down code from my class

class Q {

  public String question_1;
  public String question_2;
  public String question_3;
  public String answer_1;
  public String answer_2;
  public String answer_3;
  //etc. etc.  Many String attributes

  // … constructor and other stuff are omitted

  // here is my method for "dynamically" setting each attribute
  public void set_attribute(String a_raw_string, String my_field) {
    try {
      Class cls = Class.forName("com.xyz.models.Q");
      Field fld = cls.getField(my_field);
      fld.set(this, a_raw_string);
  }
  catch (Throwable e) {
      System.err.println(e);
  }
}

I then set various fields like this:

Q q = new Q();
q.set_attribute("abcde", "question_1");
q.set_attribute("defgh", "question_2");
// etc.

This works (i.e., the instance variables are set when I call set_attribute.

However, they only work when the instance variables are declared public. When they are declared private I get a NoSuchFieldException

QUESTION 1: Why do I get that error when the fields are private? My naive assumption is that since the set_attribute function is part of the class, it should have unfettered access to the instance variables.

QUESTION 2: I think I may be overthinking this problem (i.e., I shouldn’t be using reflection to set variables in this way). Is there a more recommended approach?
The reason that I want to use reflection is because it’s a pain in the ass to declare a ton of setter methods…so I’m wondering if someone has solved this annoyance in a better way.

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-05-17T18:19:42+00:00Added an answer on May 17, 2026 at 6:19 pm

    I think I may be overthinking this problem (i.e., I shouldn’t be using reflection to set variables in this way)

    Yep. Reflection is fairly slow and should only be used as a last resort. If this is simply to avoid having so much redundant code, consider using automatic code generation. For pure data objects, I would strongly recommend using protocol buffers; it will generate the getters / setters (you only need to declare the fields). Plus it allows for easy communication of the data between C++, Java, and Python.

    If you have a class that has a lot of fields but isn’t a pure data object… well

    1. You should consider whether all the fields should be mutable. (Do you really need setters?)
    2. Whether the fields should even be visible. (Do you need any accessors at all?)

    It is often a good idea to make fields “final”, initialize them in the constructor(s), and provide no access or provide limited access through an implemented interface.

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

Sidebar

Related Questions

Let's say I have a class called Store that has many Employees. My RESTful
I have a base class for many tests that has some helper methods they
I have a class that has a Generic type G In my class model
I have a class that has some properties. And I want something that calculates
I have a class that has a vector of another class objects as a
Let's say I have a class that has a member called data which is
I have a base class that has a private static member: class Base {
I have an entity class that has a property with an underlying db column
Let's say I have one class Foo that has a bunch of logic in
I have a page base class that has no .aspx file and so 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.