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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:40:17+00:00 2026-05-27T06:40:17+00:00

First, a clarification: By interface, I mean any point of interaction between components, not

  • 0

First, a clarification: By “interface”, I mean any point of interaction between components, not necessarily the “abstract type” kind of interface. (Although if I can use such an interface to do this, great.)

I’m writing a class which is designed to be extended. The problem domain is global optimization, so any end-user-programmer who subclasses my class (I’ll call them Steve) will be providing their own problem and algorithm to solve it; I just provide the framework (networking, user interface, etc.). Steve’s program might require any number of configuration settings; I want my code to be able to get those settings from the user the same way it gets its own settings (be that from a GUI control panel, an XML configuration file, or whatever). Steve should not have to duplicate any of my code, nor should he have to break Java’s type safety or encapsulation model.

Ideally, Steve would be able to write something along these lines:

public class SteveClass extends MyFrameworkClass {

    @Configurable
    private int foo;

    @Configurable
    private String bar;

    private boolean baz;

    // implementations of my abstract methods, etc.

}

Configurable is an annotation that I would provide, that indicates that the annotated variable is a configuration setting that I should get from the user. As you can see, I also want Steve to be able to declare other instance variables for his own internal use, that I never touch.

I was going to implement this by including a method in MyFrameworkClass that would iterate through getClass().getDeclaredFields() and identify the fields that have Configurable annotations. It would then writes the user input to those fields. It doesn’t have to look exactly like this, with annotations and all, but you get the idea.

The problem with this, of course, is that foo and bar are private and code in MyFrameworkClass can’t write to them, even reflectively. It wouldn’t help if they were package-private, since Steve’s code won’t be in the same package as mine, and I’d rather not require him to make them public, or to add any other interface that would allow anyone but me to access the fields.

Is there any way to do what I want, either by fiddling with SecurityManager or doing something completely different?

  • 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-27T06:40:18+00:00Added an answer on May 27, 2026 at 6:40 am

    You can use Class.getDeclaredFields to get all of the class’s declared fields (including non-public ones), and then Field.setAccessible(true) to gain access to it.

    Field foo = HasPrivate.class.getDeclaredField("foo");
    foo.setAccessible(true);
    SteveClass steve = new SteveClass();
    System.out.println(steve); // "0" -- I set up SteveClass.toString() to just print foo
    foo.setInt(hp, 1234);
    System.out.println(steve); // "1234"
    

    But that means you’re depending on the SecurityManager to allow you to do that. It does by default, but that may not work for all users. If that’s a prohibitive requirement, there are other, less convenient but potentially more robust options (like requiring that the constructor take a Configuration object, which the user can then get values from).

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

Sidebar

Related Questions

First off, I understand the reasons why an interface or abstract class (in the
First, it is long post so if you need clarification please let me know.
If I receive user input, does it make any difference if I validate first
FINAL NOTE Final solution found in another post Although I appreciated the clarification that
Question Clarification: I'm trying to test if the user is authenticated or not for
First of all, i want some clarification :) 1) Is RoR like a server
First, let's get the security considerations out of the way. I'm using simple authentication
First off, I am using Windows XP. I have multiple hard drives and it
First off if you're unaware, samba or smb == Windows file sharing, \\computer\share etc.
First of all, I know how to build a Java application. But I have

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.