Click here to watch in Youtube :
https://www.youtube.com/watch?v=-K4fRNejyD8&list=UUhwKlOVR041tngjerWxVccw
OuterClass.java
/* * If a declaration of a type (such as a member variable or a parameter name) * in a particular scope (such as an inner class or a method definition) has * the same name as another declaration in the enclosing scope, then the declaration * shadows the declaration of the enclosing scope. * You cannot refer to a shadowed declaration by its name alone. */ public class OuterClass { public int x = 10; class InnerClass { public int x = 500; public void innerDisplay(int x) { System.out.println("x = " + x); System.out.println("this = " + this); System.out.println("this.x = " + this.x); System.out.println("OuterClass.this = " + OuterClass.this); System.out.println("OuterClass.this.x = " + OuterClass.this.x); } } }
public class ShadowTest { public static void main(String[] args) { OuterClass outerClassObj = new OuterClass(); OuterClass.InnerClass innerClassObj = outerClassObj.new InnerClass(); innerClassObj.innerDisplay(2000); } }
x = 2000 this = OuterClass$InnerClass@3b95a09c this.x = 500 OuterClass.this = OuterClass@6ae40994 OuterClass.this.x = 10
https://sites.google.com/site/javaee4321/java/NestedClassDemo-Shadow-App.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/NestedClassDemo-Shadow-App
Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/d3069ae3fffccb0c6ba4336662b6a2619d880c61/BasicJava/NestedClassDemo-Shadow-App/?at=master
See also:
No comments:
Post a Comment