{"id":4716,"date":"2014-03-30T14:52:57","date_gmt":"2014-03-30T14:52:57","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/java-how-expensive-is-a-method-call-collection-of-common-programming-errors\/"},"modified":"2014-03-30T14:52:57","modified_gmt":"2014-03-30T14:52:57","slug":"java-how-expensive-is-a-method-call-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/java-how-expensive-is-a-method-call-collection-of-common-programming-errors\/","title":{"rendered":"java how expensive is a method call-Collection of common programming errors"},"content":{"rendered":"<p>What everyone else has said about optimization is absolutely true.<\/p>\n<p>There is no reason <em>from a performance point of view<\/em> to inline the method. If it&#8217;s a performance issue, the JIT in your JVM will inline it. In java, method calls are so close to free that it isn&#8217;t worth thinking about it.<\/p>\n<p>That being said, there&#8217;s a different issue here. Namely, it <em>is<\/em> bad programming practice to call an overrideable method (i.e., one that is not <code>final<\/code>, <code>static<\/code>, or <code>private<\/code>) from the constructor. (Effective Java, 2nd Ed., p. 89 in the item titled &#8220;Design and document for inheritance or else prohibit it&#8221;)<\/p>\n<p>What happens if someone adds a subclass of <code>BinarySearchTree<\/code> called <code>LoggingBinarySearchTree<\/code> that overrides all public methods with code like:<\/p>\n<pre><code>public void clear(){\n  this.callLog.addCall(\"clear\");\n  super.clear();\n}\n<\/code><\/pre>\n<p>Then the <code>LoggingBinarySearchTree<\/code> will never be constructable! The issue is that <code>this.callLog<\/code> will be <code>null<\/code> when the <code>BinarySearchTree<\/code> constructor is running, but the <code>clear<\/code> that gets called is the overridden one, and you&#8217;ll get a <code>NullPointerException<\/code>.<\/p>\n<p>Note that Java and C++ differ here: in C++, a superclass constructor that calls a <code>virtual<\/code> method ends up calling the one defined in the superclass, not the overridden one. People switching between the two languages sometimes forget this.<\/p>\n<p>Given that, I think it&#8217;s probably cleaner in your case to inline the <code>clear<\/code> method <em>when called from the constructor<\/em>, but in general in Java you should go ahead and make all the method calls you want.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What everyone else has said about optimization is absolutely true. There is no reason from a performance point of view to inline the method. If it&#8217;s a performance issue, the JIT in your JVM will inline it. In java, method calls are so close to free that it isn&#8217;t worth thinking about it. That being [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4716","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4716","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4716"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4716\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}