{"id":1834,"date":"2013-10-03T09:04:51","date_gmt":"2013-10-02T21:04:51","guid":{"rendered":"https:\/\/www.deltics.co.nz\/blog\/?p=1834"},"modified":"2013-10-03T09:08:18","modified_gmt":"2013-10-02T21:08:18","slug":"to-init-or-not-to-init-that-is-the-correction","status":"publish","type":"post","link":"https:\/\/www.deltics.co.nz\/blog\/posts\/1834\/","title":{"rendered":"To init() or Not to Init(), That is the Correction"},"content":{"rendered":"<span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\">&lt; 1<\/span> <span class=\"rt-label rt-postfix\">minutes]<\/span><\/span><p>The short answer is:  Yes, <strong>init()<\/strong><\/p>\n<p>In <a href=\"https:\/\/www.deltics.co.nz\/blog\/posts\/1817\">a previous post<\/a> I lamented the fact [<em>cough<\/em> &#8211; Ed] that some Java class imports appeared to support the underlying Java constructors via an <code>init()<\/code> method, where others did not.  This wasn&#8217;t a &#8220;fact&#8221; at all.  It was a misunderstanding based on an incomplete grasp of the machinations of the Java class import mechanism in the <strong>AndroidAPI<\/strong> units.<\/p>\n<p><!--more--><\/p>\n<p>In the code that was presented as part of that post, I demonstrated the construction of a <strong>JIntentFilter<\/strong> followed by separate initialisation.  This can actually be achieved more directly.<\/p>\n<p>Code completion offered only a parameterless constructor (<code>Create<\/code>) on the <strong>TJIntentFilter<\/strong> class which I incorrectly took to represent the Java class I was working with (<strong>IntentFilter<\/strong>).<\/p>\n<p>It is in fact only a wrapper around that class, constructed by the <strong>TJavaGenericImport<\/strong> base class.<\/p>\n<p>The wrapper exposes the members of the enclosed Java Class through the <strong>JavaClass<\/strong> member.  On this member are to be found the Java constructors in the form of appropriately overloaded <code>init<\/code> methods.<\/p>\n<p>So this code:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n  filter := TJIntentFilter.Create;\r\n  filter.addAction(TJIntent.JavaClass.ACTION_BATTERY_CHANGED);\r\n<\/pre>\n<p>Is more properly written as:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n  filter := TJIntentFilter.JavaClass.init(TJIntent.JavaClass.ACTION_BATTERY_CHANGED);\r\n<\/pre>\n<p>Which is even uglier and less Pascal&#8217;ly than ever, imho, but does at least mean that you can consistently apply this constructor pattern when instantiating imported Java classes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\">&lt; 1<\/span> <span class=\"rt-label rt-postfix\">minutes]<\/span><\/span> The short answer is: Yes, init() In a previous post I lamented the fact [cough &#8211; Ed] that some Java class imports appeared to support the underlying Java constructors via an init() method, where others did not. This wasn&#8217;t a &#8220;fact&#8221; at all. It was a misunderstanding based on an incomplete grasp of the machinations [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[212,4],"tags":[153,292,225,227],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1TKYv-tA","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1817,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1817\/","url_meta":{"origin":1834,"position":0},"title":"Getting the Battery Level on Android With Delphi","date":"01 Oct 2013","format":false,"excerpt":"Over the past few days I posted a two part series showing how to obtain the current battery level as part of the implementation of an Android AppWidget using Oxygene. As far as I can tell AppWidgets simply aren't possible using Delphi but reading the battery is quite straightforward Android\u2026","rel":"","context":"In &quot;Android&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1833,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1833\/","url_meta":{"origin":1834,"position":1},"title":"Importing an Android Class For Use in Delphi","date":"03 Oct 2013","format":false,"excerpt":"In a previous post I noted the absence of the BatteryManager class in the AndroidAPI.JNI units. This class contains some constants useful when reading battery information. I showed how to use a suitably massaged literal in place of these missing constants, but in response to observations from Paul and Brian\u2026","rel":"","context":"In &quot;Android&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2280,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/2280\/","url_meta":{"origin":1834,"position":2},"title":"Oxygene Constructor Magic on Cocoa","date":"30 Aug 2014","format":false,"excerpt":"Earlier this week I mentioned that I had published my TXT-2-PARK app for Android in the Google Play Store. Today I published the iOS version to the Apple App Store (still awaiting approval at this stage). As with the Android version, I implemented the iOS version using Oxygene, and things\u2026","rel":"","context":"In &quot;Android&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1713,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1713\/","url_meta":{"origin":1834,"position":3},"title":"How to Call Java Code from an Oxygene Android Application","date":"20 Sep 2013","format":false,"excerpt":"Lachlan just posted a link to a post on Google+ (also available as a PDF) demonstrating how to call Java from Delphi XE5. I was shocked at both the amount and the nature of the code involved. It is long, convoluted and ugly stuff (nb. that isn't a criticism of\u2026","rel":"","context":"In &quot;Android&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1624,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1624\/","url_meta":{"origin":1834,"position":4},"title":"Exploring Listeners With Oxygene","date":"16 Sep 2013","format":false,"excerpt":"Part 2 in a short series demonstrating the development of a simple camera app for Android using Oxygene. In the previous instalment we looked at the basic framework of our app. For this instalment I was going to show how to implement the camera preview or viewfinder for this instalment,\u2026","rel":"","context":"In &quot;Android&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1882,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1882\/","url_meta":{"origin":1834,"position":5},"title":"Not Your Grand-Daddy&#8217;s Pascal (or Java)","date":"15 Oct 2013","format":false,"excerpt":"I've mentioned some of the cool stuff in the Oxygene language in various posts and thought it would be a good idea to list them again, along with some others that I've not previously mentioned. Oxygene Everywhere First some of the core language features that are available on all supported\u2026","rel":"","context":"In &quot;Android&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1834"}],"collection":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/comments?post=1834"}],"version-history":[{"count":2,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1834\/revisions"}],"predecessor-version":[{"id":1838,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1834\/revisions\/1838"}],"wp:attachment":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/media?parent=1834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/categories?post=1834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/tags?post=1834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}