What are you trying to achieve ? According to javadoc , the get( String key ) method will always return an Object . But If you want your hashmap to always return an Integer then use parameterized generics like this
However if you look at the back-end , in this example the JVM will do the casting by itself for you , so ultimately casting has to be done since the default return type of get( String key ) is Object.
Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Paul Ralph wrote:Why am I getting Objects instead of Integers? Is there a way to cast the Objects into Integers? Am I even asking the right questions?
Because you are using the raw type HashMap, without using generics. When you use the raw type, the keys and values will be of type Object, and you need to cast them.
The cast would not be necessary if you would use generics:
This is essentially what's wrong: you're declaring a local variable with the same name as your class variable (NumberTable). The static block has its own scope just like methods do, so when you assign anything to it, your class variable won't be affected at all.