{"id":412,"date":"2007-10-21T00:00:00","date_gmt":"2007-10-21T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=412"},"modified":"2007-10-21T00:00:00","modified_gmt":"2007-10-21T00:00:00","slug":"Generic Methods and Constructors","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=412","title":{"rendered":"Generic Methods and Constructors"},"content":{"rendered":"<p><DIV><br \/>\n<DIV id=PageTitle>Generic Methods and Constructors<\/DIV><br \/>\n<BLOCKQUOTE>Type parameters can also be declared within method and constructor signatures to create <I>generic methods<\/I> and <I>generic constructors<\/I>. This is similar to declaring a generic type, but the type parameter&#8217;s scope is limited to the method or constructor in which it&#8217;s declared.<br \/>\n<BLOCKQUOTE><PRE>\/**<br \/>\n * This version introduces a generic method.<br \/>\n *\/<br \/>\npublic class Box&lt;T&gt; {<\/p>\n<p>    private T t;          <\/p>\n<p>    public void add(T t) {<br \/>\n        this.t = t;<br \/>\n    }<\/p>\n<p>    public T get() {<br \/>\n        return t;<br \/>\n    }<\/p>\n<p>    <B>public &lt;U&gt; void inspect(<B>U u<\/B>){<br \/>\n        System.out.println(&#8220;T: &#8221; + t.getClass().getName());<br \/>\n        System.out.println(&#8220;U: &#8221; + u.getClass().getName());<br \/>\n    }<\/B><\/p>\n<p>    public static void main(String[] args) {<br \/>\n        Box&lt;Integer&gt; integerBox = new Box&lt;Integer&gt;();<br \/>\n        integerBox.add(new Integer(10));<br \/>\n        integerBox.inspect(&#8220;some text&#8221;);<br \/>\n    }<br \/>\n}<br \/>\n<\/PRE><\/BLOCKQUOTE>Here we&#8217;ve added one generic method, named <CODE>inspect<\/CODE>, that defines one type parameter, named <CODE>U<\/CODE>. This method accepts an object and prints its type to standard output. For comparison, it also prints out the type of <CODE>T<\/CODE>. For convenience, this class now also has a <CODE>main<\/CODE> method so that it can be run as an application.<br \/>\n<P>The output from this program is: <\/P><br \/>\n<BLOCKQUOTE><PRE>T: java.lang.Integer<br \/>\nU: java.lang.String<br \/>\n<\/PRE><\/BLOCKQUOTE><br \/>\n<P>By passing in different types, the output will change accordingly. <\/P><br \/>\n<P>A more realistic use of generic methods might be something like the following, which defines a static method that stuffs references to a single item into multiple boxes: <\/P><PRE>    public static &lt;U&gt; void fillBoxes(U u, List&lt;Box&lt;U&gt;&gt; boxes) {<br \/>\n        for (Box&lt;U&gt; box : boxes) {<br \/>\n            box.add(u);<br \/>\n        }<br \/>\n    }<\/p>\n<p><\/PRE>To use this method, your code would look something like the following: <PRE>    Crayon red = &#8230;;<br \/>\n    List&lt;Box&lt;Crayon&gt;&gt; crayonBoxes = &#8230;;<br \/>\n<\/PRE>The complete syntax for invoking this method is:<br \/>\n<BLOCKQUOTE><PRE>Box.&lt;Crayon&gt;fillBoxes(red, crayonBoxes);<br \/>\n<\/PRE><\/BLOCKQUOTE>Here we&#8217;ve explicitly provided the type to be used as <CODE>U<\/CODE>, but more often than not, this can be left out and the compiler will infer the type that&#8217;s needed:<br \/>\n<BLOCKQUOTE><PRE>Box.fillBoxes(red, crayonBoxes); \/\/ compiler infers that U is Crayon<br \/>\n<\/PRE><\/BLOCKQUOTE>This feature, known as <I>type inference<\/I>, allows you to invoke a generic method as you would an ordinary method, without specifying a type between angle brackets. <\/BLOCKQUOTE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Generic Methods and Constructors Type parameters can also be declared within method and constructor signatures to create generic methods and generic constructors. This is similar to declaring a generic type, but the type parameter&#8217;s scope is limited to the method or constructor in which it&#8217;s declared. \/** * This version introduces a generic method. *\/ &hellip; <a href=\"https:\/\/www.strongd.net\/?p=412\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Generic Methods and Constructors<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-412","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/412","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=412"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/412\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}