I want to use a static Map in other classes. If you could please tell me which practice is better. I can make it private and provide appropriate getters and setters for key and value. On the other hand, the second option is just to declare it public and in those classes use methods from built-in Map Collection.
First option example:
public class MapClass{
private static Map<int, String> map = new HashMap<>();
public String getValueForKey(int key){
return map.get(key);
}
// other methods
}
public class DifferentClass{
public void writeString(int number){
System.out.println(MapClass.getValueForKey(number));
}
}
Second option example:
public class MapClass{
public static Map<int, String> map = new HashMap<>();
}
public class DifferentClass{
public void writeString(int number){
System.out.println(MapClass.get(number));
}
}