Reputation: 587
I have this class as follows :-
import java.util.*;
public class QueueTest<T>{
Queue<T> q = new LinkedList<T>();
int capacity;
public QueueTest(int capacity){
this.capacity = capacity;
}
**public <T> void put(T item) throws InterruptedException**{
while(q.size()== capacity){
System.out.println("Now Queue is full and i will wait");
wait();
}
q.add(item);
System.out.println("I added : "+item);
notify();
}
public T got() throws InterruptedException{
while(q.isEmpty()){
System.out.println("Now Queue is empty and i will wait ");
wait();
}
T item = q.remove();
System.out.println("I got : "+item);
notify();
return item;
}
}
the declaration of method put() cause this error ??
QueueTest.java:17: error: no suitable method found for add(T#1)
q.add(item);
and when I remove T , the class compiled without errors, but I don't know why ???
Upvotes: 0
Views: 51
Reputation: 280102
Get rid of the T
in
public <T> void put(T item) throws InterruptedException{
^
If you leave it, you are declaring a different type variable that is shadowing your class' type variable T
. You can bind a different type to this one, so the compiler cannot guarantee that it will be the same as the one bound to the Queue
. You seem to want to use the same type variable as the one the in the class declaration.
Upvotes: 3