I am writing a simple program to convert a number to a word representing that number (13 => "thirteen").
I realize I could get some of the words with a constant String array like this:
private static final String[] tensNames = {"", " ten", " twenty", " thirty", " forty", " fifty", " sixty", " seventy", " eighty", " ninety" };
...and access it with the index, but I wanted to try it with a HashMap like this:
final HashMap<Integer, String> tensNumberConversion = new HashMap<Integer, String>();
tensNumberConversion.put(2, "twenty");
tensNumberConversion.put(3, "thirty");
tensNumberConversion.put(4, "forty");
tensNumberConversion.put(5, "fifty");
tensNumberConversion.put(6, "sixty");
tensNumberConversion.put(7, "seventy");
tensNumberConversion.put(8, "eighty");
tensNumberConversion.put(9, "ninety");
I was instructed by a teacher to make these constants. Can the HashMap be a constant? As a newbie, it wasn't totally clear how the terms "constant" and "static" and "final" are related, and what exactly makes a constant. (static alone? final alone? static final together?).
I tried making it private static final Hashmap but IntelliJ gave me an error (modifier 'private' not allowed here...same for 'static').
However, it does compile from my terminal with no errors. If a HashMap can be a constant, is this the right way to declare one? Thanks!