Reputation: 581
I'm having some problems doing an HashTable in Java with an Object as value. So, I have a superclass called "Bonus" and I got some subclasses called HammerBonus, BombBonus, etc. I have created my HashTable like this:
private Hashtable<String, Bonus> hashBonusElement= new Hashtable<String, Bonus>();
But now I cant add any subclass as a value, for exemple:
hashBonusElement.put(Hammer.class.getSimpleName(), HammerBonus);
It says that "HammerBonuscannot be resolved to a variable".
I have tried somethings like "(Bonus)HammerBonus", "HammerBonus.class" but nothing works... Is there anything that I can do for make it work?
Thank you in advance for your help!
Upvotes: 0
Views: 313
Reputation: 26198
That is because you added the Class name as a value to the HashMap..
try this
hashBonusElement.put(Hammer.class.getSimpleName(), new HammerBonus());
will create a new instance of HammerBonus to add as a value to the HashMap.
Upvotes: 1