{"id":182,"date":"2010-08-09T10:41:22","date_gmt":"2010-08-09T10:41:22","guid":{"rendered":"http:\/\/colinjohnson.me.uk\/blog\/?p=182"},"modified":"2010-08-28T01:39:17","modified_gmt":"2010-08-28T01:39:17","slug":"a-question-about-java-generics","status":"publish","type":"post","link":"https:\/\/colinjohnson.me.uk\/blog\/?p=182","title":{"rendered":"A Question about Java Generics"},"content":{"rendered":"<p><em>Generics<\/em> are a useful feature of the Java programming language, allowing the programmer to create a data structure that is parameterised by a data type. I have a specific question about generics to which I can&#8217;t find the answer. Here is the setup: let me know if you have a solution to this.<\/p>\n<p>We can create a class, call it <code>Example<T><\/code>, which we plan to use with a number of different types, call these <code>T1<\/code>, <code>T2<\/code> etc. Which of these types gets used depends on a user-specified parameter, call this <code>int p<\/code>.<\/p>\n<p>Here is a fragment of pseudo-Java showing what I want to do (I have added line numbers for later discussion).<\/p>\n<p><code>1. Example X; \/\/note I haven't parameterised the generic yet<\/code><br \/>\n<code>2. switch (p)<\/code>{<br \/>\n<code>3. case 1:<\/code><br \/>\n<code>4. &nbsp; &lt;T1&gt;X = new Example(blah blah blah);<\/code><br \/>\n<code>5. break;<\/code><br \/>\n<code>6. case 2:<\/code><br \/>\n<code>7. &nbsp; &lt;T2&gt;X = new Example(blah blah blah);<\/code><br \/>\n<code>8. break;<\/code><br \/>\n<code>9. }<\/code><br \/>\n<code>10. X.printAll();<\/code><br \/>\n<code>11. X.anotherGeneralMethod();<\/code><\/p>\n<p>The difficulty here is that I cannot get how to code the idea suggested by lines 4 and 7. I need to find a way of <em>declaring<\/em> an unparameterised generic, and then parameterising it later. Is there such a way? The only way I have found to do this so far is to duplicate (or similar) the code in lines 9 and 10 to within each case in the switch statement. <\/p>\n<p>I have read about the notion of <em>wildcards<\/em>, perhaps this is what I need\u2014but I can&#8217;t see how to do the later concretization. Similar arguments apply with using <code>Object<\/code> at declaration.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Generics are a useful feature of the Java programming language, allowing the programmer to create a data structure that is parameterised by a data type. I have a specific question about generics to which I can&#8217;t find the answer. Here is the setup: let me know if you have a solution to this. We can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[22],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/colinjohnson.me.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/182"}],"collection":[{"href":"https:\/\/colinjohnson.me.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/colinjohnson.me.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/colinjohnson.me.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/colinjohnson.me.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=182"}],"version-history":[{"count":14,"href":"https:\/\/colinjohnson.me.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/182\/revisions"}],"predecessor-version":[{"id":242,"href":"https:\/\/colinjohnson.me.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/182\/revisions\/242"}],"wp:attachment":[{"href":"https:\/\/colinjohnson.me.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/colinjohnson.me.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/colinjohnson.me.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}