Generic Methods in Java

Source: Internet
Author: User

What the generics mean is not much to say, and the definition of generic classes in Java is relatively simple, for example: public class test<t>{}. This defines a generic class Test, which, when instantiating the class, must indicate the specific type of the generic T, such as:test<object> t = new test<object> (), indicating that the type of generic T is Object.

But generic methods in Java are more complex.

A generic class that specifies the specific type of a generic when it instantiates a class, and a generic method that indicates the specific type of the generic when the method is called.

The syntax format for defining generic methods is as follows:

call the generic method syntax in the following format:

Note that when defining a generic method, you must add a <t> to the return value, declaring that this is a generic method, holding a generic t, and then using the generic T as the return value of the method.

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

Why use variable c to create objects? Since it is a generic method, it means that we do not know what the specific type is or how it is constructed, so there is no way to go to the new object, but you can use the Newinstance method of the variable C to create the object, which is to use reflection to create the object.

The parameters required by the generic method are the class<t> type, and the return value of the Class.forName () method is also Class<t>, so you can use Class.forName () as the parameter. Where the parameter in the Forname () method is the type, and the class<t> that is returned is the type. In this case, the forname () method passed in the full path of the User class, so the object returned is of type class<user>, so when the generic method is called, the type of the variable C is class<user> Therefore, the generic t in the generic method is indicated as user, so the type of the variable obj is user.

Of course, a generic method may not just have a parameter class<t> You can add additional parameters as needed.

Why use a generic method? Because generic classes specify types at instantiation time, if you want to change a type, you have to re-new again, which may not be flexible enough, whereas a generic method can be more flexible when it is called.

Learn Java students pay attention to!!!
You are welcome to join the Java Learning Exchange Group when you encounter any problems in the learning process or want to acquire learning resources: 159610322 We'll learn java! together.

Generic Methods in Java

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.