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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:01:25+00:00 2026-05-26T01:01:25+00:00

I am working on a Configuration Loader class so that I can change the

  • 0

I am working on a Configuration Loader class so that I can change the parameters of my program via an external text file (config.txt) rather than having to recompile my code with every change I make.

It has been suggested that I use Java’s Reflection to do this, but I’m a little confused as to how I might actually implement this.

I have been able to successfully extract the class name and the arguments for its constructor from my text file, but how do I go from this to an instantiated object?

here’s what I have of my method so far:

public void loadObject(String classString, HashMap hm)
  {
  String className = props.getProperty(classString);
  Class c = Class.forName(className);
  }

classString is a string containing the name of the class, and hm is a hashmap where the class’ constructor parameters map to their intended values.

I.e., for class Foo (int xPos, float yPos), “xPos” would map to a string of the intended int, and “yPos” maps to a string of the intended float. I want to be able to return, new Foo(hm.get"xPos".toInt, hm.get"yPost".toFloat), but I’m unsure how to dynamically use a constructor like that (the issue is, there are multiple possible classes — perhaps it’s a bar instead of a foo, for instance).

I know that its possible to do an if/else based off the classString, and simply call the proper constructor after identifying it that way, but I am looking to create a more extensible code that doesn’t have to be rewritten every time I add a new class to the program.

All of the possible objects inherit from a single parent object.

  • 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-26T01:01:26+00:00Added an answer on May 26, 2026 at 1:01 am

    You would use Class.getConstructor(Class<?>... parameterTypes) to get a reference to the constructor followed by Constructor.newInstance(Object... initargs).

    However I would suggest taking a look at a dependency injection framework such as Spring or Guice as it sounds like what you are creating is a basic version of what they do.

    Upon request for expanding this answer:

    Class c = Class.forName(name);
    Constructor ctor = c.getConstructor(Integer.class, Integer.class);
    Integer param1 = hm.get("xPos") ...;
    Integer param2 = hm.get("yPos") ...;
    Object instanceOfTheClass = ctor.newInstance(param1, param2);
    

    Of course instead of param1 and param2 you would create an array of arguments based upon what was in the input file (the same goes for the arguments to getConstructor()), etc.

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

Sidebar

Related Questions

I am working on code re-factoring of configuration file loading part in PHP. Earlier
I'm working on a system that interacts with many external system API:s. Most of
Has anyone got this configuration working? Latest Netbeans, latest Glassfish, I created an EJB
I'm working on a configuration script for a JNI wrapper. One of the configuration
I recently read Ayende's blog post on automatic registration working with XML Configuration. I
I'm working in xmldataprovider and we have configuration value source this value may be
I am working in a project where there are configuration files, one in number
I am working on a project and I need to save user configuration. The
A project i'm working on is crashing when built with release configuration. We need
I am working on a small AIR desktop application and I have some configuration

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.