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

  • Home
  • SEARCH
  • 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 8339815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:59:12+00:00 2026-06-09T04:59:12+00:00

I have a design question: let me expalin in simple example: Public class A()

  • 0

I have a design question: let me expalin in simple example:

Public class A()
{
public static HashMap map = new HashMap();
public static String url = "default";
static {
  getJson();
}

//url getters and setters are defined

public static getJson() {
//code which uses url to get json and populate hashmap
}
public string getresult(String key) {
//uses hashmap to send result.
}

I am using static initialization block because i want to get json only once.

public class B {

//here I want to change url and call getJson method. If i call A.setUrl() then before setting url, A.getJson () method is called as it is in static initialization block.how can i set url first and then call getJson().

//is this a bad design?

}
  • 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-09T04:59:13+00:00Added an answer on June 9, 2026 at 4:59 am

    Yes, it is bad design:

    1. It is impossible to customize where A gets its data from without modifying the definition of A. Among other things, this prevents unit testing (as you probably don’t want to fail the unit test if the network is unavailable …).
    2. If initialization fails (for instance because the remote URL is currently unavailable), you can’t easily catch that exception, as you don’t know which access triggered loading. You can’t throw a checked exception from a static initializer. You can’t retry initialization either (all subsequent access immediately result in an exception).

    If you must access A through a static field, I’d recommend:

    public class A {
        private static Map<String, String> map;
    
        /** must be invoked before get is first called */
        public static void init(Map<String, String> newmap) {
            map = newmap;
        }
    
        public static String get(String key) {
            return map.get(key);
        }
    }
    

    This separates the concern of using the data from the concern of obtaining it, allowing each to be replaced and tested independently.

    Also consider getting rid of static, as it enforces there is only ever one map in your entire application at the same time, which is quite inflexible. (See the second code sample in Ajay’s answer for how)

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

Sidebar

Related Questions

I have a design problem: Question class: have a question and answer (String) TextQuestion
This is more of a design question. I have a template class, and I
For my new Pet-Project I have a question for design, that is decided already,
My question is about software architecture in general. Let's consider an example: We have
I'd better ask the question by example. Let's say I have UserControl and Window
I have a design question. We need to put some default, system defined content
I have a design/best practice question relating to MVC3. I have a front page
This is more of a general design question I guess... I have an Ajax
More of a design/conceptual question. At work the decision was made to have our
This is probably more a design or style question: I have just been considering

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.