1

Sorry for this simple question

In this class

class GenericTest{
    static <T> List<T> getList(List<T> list){
        return list;
    }
}

why this this <T> just after static needed in the declaration. I thought the return type List<T> is fine.

1 Answer 1

7

You are declaring that this method has a type parameter "T". If you didn't declare it then there is no way for the compiler to know what type you're talking about (T is not declared anywhere else).

Sign up to request clarification or add additional context in comments.

1 Comment

Sjr is absolutely right. But it's worth pointing out there are two ways you can use generic in Java. You can have generic methods and generic classes. The above is an example of a generic method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.