Solved

static maps

Posted on 2012-03-14
2
185 Views
Last Modified: 2012-03-24
How do I implement a static map class to be used by a few other classes?
examples to follow will be great.
0
Comment
Question by:dpalyca755
2 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37721968
can you please elaborate?

HashMap as any Java object can be made static and then you could address it by refereinng to the class name rather than the instance of the class where it is a field
So if you declare it like that:
class ClassName {
public static HashMap<Integer, String> map;
...


}

Thane in any other class of your applucation you can address it like that:

ClassName.map.get(key)

thsi is no different form any other static object
0
 
LVL 7

Accepted Solution

by:
gudii9 earned 500 total points
ID: 37722491
Here is interesting explanation, links

public static Map CONSTANTS = new HashMap() { {
put("key1", "value1");
put("key2", "value2");
}};


Even better is to wrap the ArrayList/HashMap by passing them to Collections.unmodifiableList or Collections.unmodifiableMap. Thus making sure that it is truly a constant list/map.

http://livinginjava.blogspot.com/2010/08/initialize-static-list-and-map.html
http://stackoverflow.com/questions/507602/how-to-initialise-a-static-map-in-java
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question