Solved

static maps

Posted on 2012-03-14
2
181 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now