Is this java generic method?
generics, java, list, methods
Solution
No, your `toList` is not a generic method.
The Java Language Specification, Java SE 8 edition, Section 8.4.4 says:
A method is generic if it declares one or more type variables.
So a generic method has type parameters of its own, but your `toList` only uses the type parameters of the class. Hence it is not a generic method.
This is also explained in the tutorial linked to in the comment by Lutz Horn. The section on generic methods says:
Just like type declarations, method declarations can be generic—that is, parameterized by one or more type parameters.
This confirms that a method is generic if it has type parameters of its own.
Here is an example for a generic method:
public static <T> List<T> boxToList(Box<T> box) {
return box.toList()
}
Problem
``` public Box<T>{ private T t; public void setBox(T t){ this.t = t; } public List<T> toList(){ List<T> list = new ArrayList<>(); list.add(t); return list; } } ``` In this simple code is this `toList()` method generic or not? Thank you...