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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:09:22+00:00 2026-05-20T15:09:22+00:00

I have a class that reads a configuration file once and puts all the

  • 0

I have a class that reads a configuration file once and puts all the parameters (key/value pairs) in a Map.

For retrieving values from the Map instance I have created a method that takes a key and return the corresponding value.
The config map is passed to all the classes that need to read from the configuration. Each class that uses it reads the configuration parameters it needs and stores them in instance variables which are referred to in the rest of the class.

Is it correct to use an instance variable and store the value in that or should I call the accessor method on the config object whenever I am required to get the value for any 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-05-20T15:09:23+00:00Added an answer on May 20, 2026 at 3:09 pm

    First, your configuration class should consider using java.util.Properties for its loading (and saving if necessary.) It’s very convenient.

    Second, what you’re doing is dependency injection, which is a good thing. You make it clear that “This class uses –THESE– configuration settings.” I think what you’re doing is right.

    It sounds like you’re caching these values in your class rather than looking them up from your configuration. That’s fine under the following conditions

    1. The configuration won’t change at runtime. OR
    2. If the configuration DOES change, you don’t want to change the instance of the class.

    Example of two:

    void spawnClass() {
       MyProps config = magicConfigurationLoad();
       MyClass a = new MyClass(config); // caches property xyz
       config.setProperty("xyz","bbq"); // change property xyz
       MyClass b = new Class(config); // caches the new value of xyz
    }
    

    In any event, this is why you only reference instance variables in your get() methods and always use get() methods, even in internal methods.

    Consider the internal workings of MyClass

    class MyClass {
    
       static final String IMPORTANT_PROP_KEY = "xyz";
       String myProp;
    
       MyClass(MyProps props) {
           this.myProp = props.get(IMPORTANT_PROP_KEY);
       }
    
       void myFunc() {
           System.out.println("My prop is " + myProp); // bad
       }
    
    }
    

    Now let’s say you want to change the implementation of MyClass so it references a MyProps instance so it can change on the fly (as in, #2 above doesn’t apply, you want to change behavior as you go

    class MyClass {
        static final String IMPORTANT_PROP_KEY = "xyz";
        MyProps props;
    
        MyClass(MyProps props) {
           this.props = props;
       }
    
       void myFunc() {
           System.out.println("My prop is " + props.get(IMPORTANT_PROP_KEY)); // ugly
       }
    
    }
    

    The class would have been better from the start if we made our class use the getter method, even if it’s private. Getters do not need to be public if you don’t want

    class MyClass {
    
       static final String IMPORTANT_PROP_KEY = "xyz";
       String myProp;
    
       MyClass(MyProps props) {
           this.myProp = props.get(IMPORTANT_PROP_KEY);
       }
    
       void myFunc() {
           System.out.println("My prop is " + getMyProp()); // good
       }
    
       private String getMyProp() {
           return myProp; // good
       }
    
    }
    

    This way when we go to change the behavior to be not cached, but instead dynamic to the props file, we can do this:

    class MyClass {
        static final String IMPORTANT_PROP_KEY = "xyz";
        MyProps props;
    
        MyClass(MyProps props) {
           this.props = props;
       }
    
       // this function didn't change
       void myFunc() {
           System.out.println("My prop is " + getMyProp()); // good
       }
    
       private String getMyProp() {
              return props.get(IMPORTANT_PROP_KEY); // good
       }
    
    }
    

    Now granted, this was a small example with one instance. Most of your classes will have 20 or 30 places where you’d have to change it. This makes it much easier to refactor your code, even if the getters are private.

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

Sidebar

Related Questions

I have a large-ish project that contains one class that reads from a file
I have a text file and a class (Reader) that reads the text file
I have a configuration class in my objective-c app which reads a PLIST file
I have a ConfigParameter SLSB that reads configuration values from the ejb-jar.xml file. This
Problem in words: For my application, I have a class that reads from a
So I have the following situation. I have a configuration class config.py that holds
I have a configuration class that maps web.config, something like that: public class SiteConfigurationSection
I have some text configuration file that need to be read by my program.
I have a dynamic class that serves as a storage container for configuration settings.
I have a service application that on startup reads an XML file and starts

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.