Generic methods in Java

Source: Internet
Author: User

The meaning of generics is not much mentioned here, and the definition of generic classes in Java is also relatively simple, for example: public class test <t> {}. In this way, a generic class test is defined. when instantiating this class, you must specify the specific type of the generic T, for example: test <Object> T = new test <Object> (); indicates that the type of generic T is object.

However, generic methods in Java are complicated.

A generic class specifies the specific type of a generic class when instantiating a class. A generic method specifies the specific type of a generic class when calling a method.

 

Define generic methodsThe syntax format is as follows:

Call generic methodsThe syntax format is as follows:

 

Note: when defining a generic method, you must add a <t> in front of the returned value to declare that this is a generic method, holding a generic t, then, you can use generic T as the return value of the method.

The function of class <t> is to specify the specific type of the generic type. The class <t> type variable C can be used to create the object of the generic class.

Why use variable C to create an object? Since it is a generic method, it means we do not know what the specific type is or how the constructor is. Therefore, there is no way to create an object, however, you can use the newinstance method of variable C to create an object, that is, using reflection to create an object.

Generic methods require the class <t> type, and the class. forname () method returns the class <t> type. Therefore, class. forname () can be used as the parameter. The type of the parameters in the forname () method and the returned class <t> are the types. In this example, the forname () method imports the complete path of the user class, so the returned class <user> type object. Therefore, when you call a generic method, the type of variable C is class <user>, so generic T in the generic method is specified as user, so the type of variable obj is user.

Of course, generic methods do not only have one parameter class <t>. You can add other parameters as needed.

Why should we use generic methods? Because generic classes need to specify the type during instantiation, if you want to change the type, you have to re-create it once, it may not be flexible enough; the generic method can specify the type when calling, which is more flexible.

 

Appendix: JavaGeneric method demonstrationCode.

Click to download

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.