{"id":1754,"date":"2016-09-23T18:04:19","date_gmt":"2016-09-23T18:04:19","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1754"},"modified":"2016-09-23T18:04:19","modified_gmt":"2016-09-23T18:04:19","slug":"calling-static-method-using-null-reference","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/calling-static-method-using-null-reference\/","title":{"rendered":"Calling Static Method using Null Reference"},"content":{"rendered":"<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Why {\r\n\r\n  public static void test() {\r\n    System.out.println(&quot;Passed&quot;);\r\n  }\r\n\r\n  public static void main(String&#x5B;] args) {\r\n    Why NULL = null;\r\n    NULL.test();\r\n  }\r\n}\r\n<\/pre>\n<p>test() is a static method. A static member belongs to the type, and do not require an instance to access.<\/p>\n<p>A static member should ONLY be accessed via a type expression. That is, you should&#8217;ve written it as follows:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nWhy.test(); \/\/ always invoke static method on the type it belongs to!\r\n<\/pre>\n<p>Java does allow you to access a static member via an object reference expression, but this is VERY misleading, since this is NOT the actual semantics of a static member access.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nWhy aNull = null; \r\naNull.test(); \/\/ DO NOT EVER DO THIS!\r\n\/\/ invokes Why.test(), does NOT throw NullPointerException\r\n<\/pre>\n<p><strong>When accessing a static member through an object reference expression, only the declared type of the reference matters<\/strong>. This means that:<\/p>\n<ol>\n<li>It doesn&#8217;t matter if the reference is actually null, since no instance is required<\/li>\n<li>If the reference is not null, it doesn&#8217;t matter what the runtime type of the object is, there is no dynamic dispatch!!!<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>public class Why { public static void test() { System.out.println(&quot;Passed&quot;); } public static void main(String&#x5B;] args) { Why NULL = null; NULL.test(); } } test() is a static method. A static member belongs to the type, and do not require an instance to access. A static member should ONLY be accessed via a type expression.&hellip; <a href=\"https:\/\/codethataint.com\/blog\/calling-static-method-using-null-reference\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[193,104],"tags":[],"class_list":["post-1754","post","type-post","status-publish","format-standard","hentry","category-interview-questions-java","category-static-and-final"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1754","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1754"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1754\/revisions"}],"predecessor-version":[{"id":1755,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1754\/revisions\/1755"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}