{"id":1283,"date":"2012-07-07T23:12:53","date_gmt":"2012-07-07T21:12:53","guid":{"rendered":"http:\/\/doanduyhai.wordpress.com\/?p=1283"},"modified":"2012-07-07T23:12:53","modified_gmt":"2012-07-07T21:12:53","slug":"variable-shadowing","status":"publish","type":"post","link":"https:\/\/www.doanduyhai.com\/blog\/?p=1283","title":{"rendered":"Variable shadowing"},"content":{"rendered":"<p>Today we&#8217;ll look into a rare feature of Java: <strong>variable shadowing<\/strong><\/p>\n<p>First, let&#8217;s define what is a shadowed field or method:<\/p>\n<blockquote><p>A field is considered <strong>shadowed<\/strong> when<\/p>\n<ul>\n<li>a subclass of its declaring class declares a field with the <strong>same name and same<\/strong><\/li>\n<li>a variable having the same name and type is declared in the local scope<\/li>\n<li>a method argument\/parameter is declared with a same name and type<\/li>\n<\/ul>\n<\/blockquote>\n<p><!--more--><\/p>\n<h1>I Local variable shadowing<\/h1>\n<pre class=\"brush: java; highlight: [7]; title: ; notranslate\" title=\"\">\npublic class MyClass\n{\n\tprivate int count = 10;\n\n\tprivate void localVariable()\n\t{\n\t\tint count = 5;\n\n\t\tSystem.out.println(&amp;quot;count = &amp;quot;+ count);\n\t}\n\n\tpublic static void main(String[] args)\n\t{\n\t\tMyClass test = new MyClass();\n\t\ttest.localVariable();\n\t}\n}\n<\/pre>\n<p> The above code will output<\/p>\n<blockquote><p>count = 5<\/p><\/blockquote>\n<p>because the <em>count<\/em> <strong>local variable<\/strong> declared at <strong>line 7<\/strong> shadows the variable <em>count<\/em> declared at class level. If we want to access the instance variable, we need to add the <strong>this<\/strong> keyword.<\/p>\n<pre class=\"brush: java; highlight: [5]; title: ; notranslate\" title=\"\">\nprivate void localVariable()\n{\n\tint count = 5;\n\n\tSystem.out.println(&amp;quot;count = &amp;quot;+ this.count);\n}\n<\/pre>\n<p>&nbsp;<\/p>\n<h1>II Method argument shadowing<\/h1>\n<p>This situation is very common even though we do not pay much attention to it. Below is a simple getter definition<\/p>\n<pre class=\"brush: java; highlight: [5]; title: ; notranslate\" title=\"\">\nprivate int count;\n\npublic void setCount(int count)\n{\n\tthis.count = count;\n}\n<\/pre>\n<p> The <strong>this<\/strong> keyword is mandatory to resolve the ambiguity. Without <strong>this<\/strong>, the compiler cannot know whether we are assigning the <em>count<\/em> method argument value to itself. If you remove the <strong>this<\/strong> keyword, you would get a compilation warning anyway.<\/p>\n<p>&nbsp;<\/p>\n<h1>III Superclass field shadowing<\/h1>\n<p>Let&#8217;s consider the following classes:<\/p>\n<pre class=\"brush: java; highlight: [3,7,13]; title: ; notranslate\" title=\"\">\npublic class SuperClass\n{\n\tprotected String val = &amp;quot;SUPER_VAL&amp;quot;;\n\n\tprotected void display()\n\t{\n\t\tSystem.out.println(&amp;quot;val = &amp;quot;+this.val);\n\t}\n}\n\npublic class ChildClass extends SuperClass\n{\n\tprivate String val;\n\n\tpublic ChildClass(String value) {\n\t\tthis.val = value;\n\t}\n\n\tpublic static void main(String[] args)\n\t{\n\t\tChildClass child = new ChildClass(&amp;quot;CHILD_VAL&amp;quot;);\n\t\tchild.display();\n\t}\n\n}\n<\/pre>\n<p>The execution gives:<\/p>\n<blockquote><p>val = SUPER_VAL<\/p><\/blockquote>\n<p> The <em>val<\/em> field has been declared in the <strong>SuperClass<\/strong> but is shadowed in the <strong>ChildClass<\/strong> because the latter declares another field with same <strong>name and type<\/strong>. Although the <strong>ChildClass<\/strong> has been instantiated with &#8220;CHILD_VAL&#8221;, the execution of <em>child.display()<\/em> gives you &#8220;SUPER_VAL&#8221;.<\/p>\n<p> The reason is simple. When the child instance is created, there are 2 variables <em>val<\/em>. The one from <strong>SuperClass<\/strong> with value &#8220;SUPER_VAL&#8221; and the one from <strong>ChildClass<\/strong> with injected value &#8220;CHILD_VAL&#8221; through constructor.<\/p>\n<p> When the <em>display()<\/em> method is called, since it is define in the <strong>SuperClass<\/strong>, it is the <em>val<\/em> field in the <strong>context of SuperClass<\/strong> which is used. Not surprising that the output shows &#8220;SUPER_VAL&#8221;.<\/p>\n<pre class=\"brush: java; highlight: [7]; title: ; notranslate\" title=\"\">\npublic class ChildClass extends SuperClass\n{\n\tprivate String val;\n\n\tpublic ChildClass(String value) {\n\t\tthis.val = value;\n\t\tsuper.val = value;\n\t}\n\n\tpublic static void main(String[] args)\n\t{\n\t\tChildClass child = new ChildClass(&amp;quot;CHILD_VAL&amp;quot;);\n\t\tchild.display();\n\t}\n\n}\n<\/pre>\n<p>In the above modified code, we force the value for the hidden <em>val<\/em> field in <strong>SuperClass<\/strong> with <em>super.val = value<\/em>, and the output gives:<\/p>\n<blockquote><p>val = CHILD_VAL<\/p><\/blockquote>\n<p>Now let&#8217;s add another class in the hierarchy<\/p>\n<pre class=\"brush: java; highlight: [17]; title: ; notranslate\" title=\"\">\npublic class AncestorClass\n{\n\tprotected String val = &amp;quot;ANCESTOR_VAL&amp;quot;;\n}\n\npublic class SuperClass extends AncestorClass\n{\n\tprotected String val = &amp;quot;SUPER_VAL&amp;quot;;\n}\n\npublic class ChildClass extends SuperClass\n{\n\tprivate String val = &amp;quot;CHILD_VAL&amp;quot;;\n\n\tpublic void displayVal()\n\t{\n\t\tSystem.out.println(&amp;quot;val = &amp;quot; + super.val);\n\t}\n\n\tpublic static void main(String[] args)\n\t{\n\t\tChildClass child = new ChildClass();\n\t\tchild.displayVal();\n\t}\n}\n<\/pre>\n<p>Obviously, the output will display<\/p>\n<blockquote><p>val = SUPER_VAL<\/p><\/blockquote>\n<p> The question now is: <strong>what if we want to display the <em>val<\/em>  value of AncestorClass<\/strong> ? Obviously the <strong>super<\/strong> keyword only refers to the first parent class up in the class hierarchy.<\/p>\n<p> Casting comes to the rescue. Indeed we can force the <strong>this<\/strong> keyword representing the current class instance to a particular type in the class hierarchy !<\/p>\n<pre class=\"brush: java; highlight: [7]; title: ; notranslate\" title=\"\">\npublic class ChildClass extends SuperClass\n{\n\tprivate String val = &amp;quot;CHILD_VAL&amp;quot;;\n\n\tpublic void displayVal()\n\t{\n\t\tSystem.out.println(&amp;quot;val = &amp;quot; + ((AncestorClass) this).val);\n\t}\n\n\tpublic static void main(String[] args)\n\t{\n\t\tChildClass child = new ChildClass();\n\t\tchild.displayVal();\n\t}\n}\n<\/pre>\n<p> This time, we do have<\/p>\n<blockquote><p>val = ANCESTOR_VAL<\/p><\/blockquote>\n<p>&nbsp;<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we&#8217;ll look into a rare feature of Java: variable shadowing First, let&#8217;s define what is a shadowed field or method: A field is considered shadowed when a subclass of its declaring class declares a field with the same name&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/www.doanduyhai.com\/blog\/?p=1283\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22,6],"tags":[],"_links":{"self":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1283"}],"collection":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1283"}],"version-history":[{"count":0,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1283\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}