Shadowing:

Having local variables and instance variables with same name is called as shadowing. If we use same names for local variables and instance variables, then local variables will be given high priority. Program:

Can you find what will be the output of below program?

           class Square
           {
             int len;
	     public Square(int len)
	     {
             len=len; //shadowing
	     }
	     public void findArea()
	     {
             System.out.println(len*len);
	     }
           }
           public class MainClass
           {
	     public static void main(String[ ] args)
	      {
		Square s=new Square(4);
		s.findArea();
	       }
           }
In the above program, since we are using same variables names for local variable in constructor and instance variable, so local variable will be given high priority. Result of that will be instance variable will have value 0 in it. So when we print the area it will be 0. This is the side effect of shadowing problem.

How to solve shadowing problem?

There are 2 solutions for this. 1. Either use different variable names for local and instance variables. 2. Or use "this" operator to distinguish between local and instance variables. Note: When we use this operator, java understands that it is instance variable. Below is the program after solving shadowing problem with solution 2.
shadowing problem

"this" Operator:

this operator says it is instance member. "this" operator will have address to current object which is calling that function. For every instance function or constructor java will automatically pass "this" pointer as the first parameter. Using "this" operator we can call one constructor from other constructors of the same class.
Example: Sample program on how to use this() to call other constructors of the same class
           class Pen
           {
	     int ink;
	    String color;
	    public Pen(String c)
	    {
		color=c;
		ink=100;
	     }
	     public Pen()
             {  
		this("black");
	      }
             }
               public class MainClass
              {
	       public static  void main(String[ ] args)
	       {
	       Pen p=new Pen();
	       Pen q= new Pen();
	       }
              }

Program:

Write program to find the area of rectangle and square.

areaofrectangle
File name : MainClass.java
        class RectSQ
        {
	int len,bred;
	public RectSQ(int l)
	{
          this(l,l);//calling below constructor
	}
	public RectSQ(int l,int b)
	{
           len =l;
	   bred=b;
	}
	public void findArea()
	{
           System.out.println(len*bred);
	}
        }
        public class MainClass
        {
	public static void main(String[ ] args)
	{ 
	    RectSQ s = new RectSQ(5);//this is square
            s.findArea();
	    RectSQ r = new RectSQ(4, 3);//this is rectangle
	    r.findArea();
	 }
       }
Note: 1. When we are calling other constructors using "this" operator, it should be the first line in the constructor, else it will give compile time error.
2. We can call a constructor only from other constructors.
3. Having a constructor infinite loop is a compile time error.