I have generic method which looks something like this.
public static <T> T addAndReturn(T element, Collection<T> collection){
collection.add(element);
return element;
}
When I call
String stringElement = "stringElement";
List<Object> objectList = new ArrayList<Object>();
Object theElement = addAndReturn(stringElement, objectList);
It infers the type as object.
When I call
Object objectElement = new Object();
List<String> stringList = new ArrayList<String>();
Object theElement = addAndReturn(objectElement, stringList);
the compiler reports error.
What are the rules behind this?