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;
}
}
Declare the variable as a static member of the class, then populate it in the static block:
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.