Monday, 11 January 2016

Java Tutorial : Java nested class shadow test


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);
        }
    }

}
ShadowTest.java
public class ShadowTest
{

    public static void main(String[] args)
    {
        OuterClass outerClassObj = new OuterClass();
        OuterClass.InnerClass innerClassObj = outerClassObj.new InnerClass();
        innerClassObj.innerDisplay(2000);
    }

}
Output
x = 2000
this = OuterClass$InnerClass@3b95a09c
this.x = 500
OuterClass.this = OuterClass@6ae40994
OuterClass.this.x = 10
Click the below link to download the code:
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:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment