Generic methods: static and non-stationary

Directly from this java tutorial:

For static general methods, a section of type parameters should appear before the return type of the method.

Is this not true for a non-stationary general method? If not what is the syntax of a non-stationary generalized method? Thanks in advance.

+6
source share
3 answers

The syntax for declaring non-static general methods is the same as for static methods, but without the static : generic parameters are placed before the return type.

 class Example { public <E> void method(E param) { } } 

Non-static methods can also use the generic type parameter of the enclosing class, as shown below. They are not considered as general methods; a common method is a method that declares type parameters .

 class Example<T> { // Not a generic method! public void method(T param) { } } 
+10
source

This is true for any general methods.

 public <T> T f() { return this.<T> f(); } 
+7
source

This statement is true for all common methods, because this is the very definition of a general method — a general method is one that declares type parameters.

+2
source

Source: https://habr.com/ru/post/948144/


All Articles