0 votes

Invoke static method on generic with generic class

I am trying to create a static method that is parameterized. I have this code:

class F<T> {
    static <T> void visualizar(Collection<T> lista) {
        for (T elem: lista) {
            System.out.println(elem);
        }
    }
}

How can I invoke this method because something like that does not work:

public class Prueba {

    public static void main(String[] args) {
        F<Integer> f = new F<>();
        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(1);
        integers.add(45);
        integers.add(2);
        F<Integer>.visualizar(integers); // ERROR
    }
}

Thank you in advance.

A cordial greeting to you.

1voto

Evgeni Enchev Points 668

So you are going to work:

F.visualizar(integers);

There is No need to put the type, this is when you're going to create an object (that is a directive to the compiler). To run a static method is not necessary.

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X