I have a method that usually takes an item from a list and has a signature of:
myMethod(T item)
I want to use this method but I know what I am sending the method.
SpecificItem myItem = new SpecificItem();
myMethod((T) myItem);
This doesn’t sit well with me. Is this a sign of bad code?
myMethodis defined in a generic class, somewhat like:If you instantiate this class, you exchange the variable type
Twith a real one:And if you call
myMethodon this instance, you have to provide aSpecificItem, becauseSpecificItemis the generic type for this instance.(I’m not sure it my post answers your question, please comment so I can improve it)