In the generalized class, the type parameter realizes the possibility of forming generalized methods relative to this type parameter. Is it possible to form a generalized method in a non-generalized class?

    2 answers 2

    Yes, it is possible.

    public class Main { public static <T> void add(ArrayList<T> arrayList, T value) { arrayList.add(value); } public static void main(String[] args) { ArrayList<String> stringArrayList = new ArrayList<>(); stringArrayList.add("First"); } } 

    Parameterization by any type of class makes it possible to use this type for class fields. In the methods, you can use generalizations regardless of whether the class is generalized or not.

      Yes. You can make a generic method in any class.

       public <T> T getFirst(List<T> list) { return list.get(0); }