Simple use of Java generics

Source: Internet
Author: User
Tags uppercase letter

Effecttive Java has always recommended the use of generics, a simple look at the use of generics

package cn.com.fzk;import java.util.arraylist;import java.util.List; public classTest1 { public Static classInnerstaticgeneric<t>{t t; Innerstaticgeneric<T> instance =NewInnerstaticgeneric<t>();  public Static<T> innerstaticgeneric<t>getinstance () {return NewInnerstaticgeneric<t>(); }    }     public Static voidmain (string[] Args) {String a="a";        GENERICTEST.W (a); Integer b=1;            GENERICTEST.W (b); }}classGenerictest<t> {     publicgenerictest () {} t t; List<T> list =NewArraylist<t>();  public voidadd (t t) {list.add (t); }     publicTGet(intIndex) {        returnList.Get(index); }     public Static<T> generictest<t>getinstance () {return NewGenerictest<t>(); }     public Static<W>voidW (w) {System. out. println (w.getclass (). getName ()); }     public Static<Q>voidq (q) {System. out. println (q.getclass (). getName ()); }     public voidT (t) {System. out. println (t.getclass ()); }

Public <E> void e (e E) {
System.out.println (e.getclass ());
}

}

When you define a generic class, the type in the,<> is written in a random, uppercase Letter. The type of a generic that is used in a non-static method does not need to be typed if it is the same as the uppercase letter of the defined generic class. If the type of the generic in the method does not define the type that needs to be developed in the method, the <E> in the E () method is equivalent to a temporary definition of type E, if not defined by the compilation pass.

In a static method, if you want to use generics, you must define this temporary type in front of it. If a static method is defined in a generic class, the generics and generics defined in the static method are Meaningless. Generics in Java itself is a syntactic sugar, at compile time has been compiled into the actual type, is a dynamic translation type, and static method is not compiled before the class is compiled, that is, all the types of static methods are already Determined. Using static methods in a generic class to manipulate my feelings is meaningless, the only possible reason is that this static method should theoretically belong to this class (a single duty). It also shows that in any non-generic class, you can define a generic method. It is also possible to create a class and method as Follows. In this case, it does not make sense to define a generic class.

class generictest {    publicstaticvoid  w (w) {        System.out.println ( W.getclass (). getName ());    }      public Static void q (q q) {        System.out.println (q.getclass (). getName ());    }      public void e (e e) {        System.out.println (e.getclass ());}    }

If you want to define a generic class that uses this generic class and wants the generic class to be defined as a generic, then do not use a static method to manipulate generics, and new out of the generic class calls the appropriate Method. It also shows that the generic class cannot do the static method tool class. This is how I feel. Don't know if there is a problem.

Simple use of Java generics

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.