I have class Counter with a method add which will count instances of any classes when creating but I get "The method add(Class) in the type Counter is not applicable for the arguments (MyClass)" How can I fix it? What should I type instead of Class<?> c
?
public class Counter
{
static Map<String, Integer> map = new HashMap<String, Integer>();
public static void add(Class<?> c)
{
String name = c.getClass().getSimpleName();
int count = list.containsKey(name) ? map.get(name) : 0;
map.put(name, count + 1);
}
}
public class MyClass
{
public MyClass()
{
Counter.add(this);
}
I agree with @Rohit Jain that it should be Object
. I however want to suggest parameterized version of add()
method:
public static <T> add(T obj) {
Class<?> clazz = obj.getClass();
// your implementation
}
Parameterized versions look better.