How do you solve the above problem? You can declare the pad method as a generic method. Just as you can specify type parameters for a class, you can specify type parameters that exist for the scope of a method: public static <T> void pad(List<T> list, T object, int count) { for (int i = 0; i < count; i++) list.add(object); } The compile can extract, or infer, the type for T based on the arguments passed to pad. It uses the most specific type that it can infer from the arguments. Your test should now pass. Generic method type parameters can also have upper bounds. You will need to use generic methods when you have a dependency between an argument to a method and either another argument or the return type. Otherwise, you should prefer the use of wildcards. In the pad method declaration, the object parameter's type is dependent upon the type of the list parameter. |