简体   繁体   中英

Java Generic Methods and constructors

I am new to java.I was going through a tutorial on java generics.

Java Generics tutorial There is a section about declaring generic methods and constructors.

Methods and constructors can be generic if they declare one/more type variables.

public static <T>T getFirst (List<T> list)

This method accepts a reference to List and will return an object of type T.

why is there a T after static? Is this a printing mistake.?

What does that mean?

I am a beginner in java an new to the concepts of generics.

The T before the method name means that returns a value of type T , where T is the template used in the method. If your method will return an int , it would look like:

public static <T> int getFirst (List<T> list)

T java.lang.Class Type参数,添加了此type参数。它只是向编译器说类型检查。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM