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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:53:46+00:00 2026-06-01T13:53:46+00:00

I have intialized Hash map inside static block, I need to access the hashmap

  • 0

I have intialized Hash map inside static block, I need to access the hashmap object to get the value using key it inside my getExpo method.

My class goes here

public class ExampleFactory {
  static
  {
    HashMap<String,Class<?>> hmap = new HashMap<String,Class<?>>();

    hmap.put("app", application.class);
    hmap.put("expo", expession.class);
  }

  public void getExpo(String key,String expression)
  {
    // I need to access the object in static block

    Class aclass=hmap.get(key); // it works when i place it inside main method but
                                // not working when i place the create object for
                                // Hashmap in static block

    return null;
  }
}
  • 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-01T13:53:48+00:00Added an answer on June 1, 2026 at 1:53 pm

    Declare the variable as a static member of the class, then populate it in the static block:

    public class ExampleFactory
    {
      // declare hmap
      static HashMap<String,Class<?>> hmap = new HashMap<String,Class<?>>();
      static
      {
        // populate hmap
        hmap.put("app", application.class);
        hmap.put("expo", expession.class);
    
      }
      //...
    }
    

    After this you can access it from inside your class.

    Delcaring the variable within the static block makes it unaccessible from outside that block. Declaring it as member of the class makes it accessible to the class.

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

Sidebar

Related Questions

I have a class in which i have intialized hashmap in static block. Passing
I have some static initializer code: someMethodThatTakesAHashMap(new HashMap<K, V>() { { put(a,value-a); put(c,value-c);} });
I have a final non-static member: private final HashMap<String,String> myMap; I would like to
I have a question related to Key Type initialization in HashMap. For example, I
e.g. I have a need where my ArrayList should contain hashmap at each index,
I have a Hashmap which has X number of elements I need to move
I need some guidance on how to initialize this hash inside a singleton. So
Map<K,V<List<K>> graph = new HashMap<K,V<List<K>>(); Are there any major obstacles to using this to
I have a block of code that basically intializes several classes, but they are
I have a C programming question on the implementation of a hash table. 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.