Reputation: 53
I'm trying to call:
public class Counter<MyClassSelectorObject> {
final HashMap<MyClassSelectorObject, Integer> counts = new HashMap<>();
public void add(MyClassSelectorObject t) {
counts.merge(t, 1, Integer::sum);
t.setCount(counts.get(t));
}
...
}
However I'm getting the following error from my compiler in Eclipse on t.setCount(counts.get(t));
:
The method setCount(Integer) is undefined for the type MyClassSelectorObject
However I have
public class MyClassSelectorObject implements Comparable<MyClassSelectorObject>{
...
public void setCount(Integer value) {
this.count = value;
}
...
}
The solution that Eclipse's quickfix feature offers me is to cast t to an Object, which makes no sense to me. Then again, I'm just getting back into coding after years away so I might be missing something basic here.
I'm using Java 8 and Eclipse Luna.
Thanks!
Upvotes: 1
Views: 269
Reputation: 61148
What you have done is declared a generic class that has a generic type parameter:
public class Counter<T> {
}
Except your T
is called MyClassSelectorObject
...
So you have shadowed your class
with a generic type parameter, you method is equivalent to:
public void add(T t) {
counts.merge(t, 1, Integer::sum);
t.setCount(counts.get(t));
}
As there are no bounds on T
, the T
is equivalent to Object
.
As you have already noted, removing the generic type from Counter
fixes your problem. Which begs the questions - why did you add it in the first place ...?
Upvotes: 3