Google+

75. 'this' keyword






Its illegal in Java to declare the two local variables with the same name inside the same enclosing scopes  (same enclosing scopes means inside the same class, same method etc).

Example for illegal program which uses the same name for instance variables and partemertrized local variables of constructor:

class Box( )
{
     double width;
     double height;
     double depth;
   
      Box(double width, double height, double depth)
      {
           width = width;
           height = height;
           depth = depth;
      }

}

If you clearly observe this program, you can understand that instance variable names marked in blue color and variable names of parameters of Box( ) constructor marked in green color are same. This is illegal and hence this program wont run in Java.

Why the programmer want to give the same name to the parameter variables ?

Its for clarity. If you give 'w' instead of 'width', the code wont be readable and we wont be able to understand what w stands for if we have used w instead of width.

How to overcome this problem? Can we have the same name for instance variable and parameter variables in the above example ?

Yes, we've to use 'this' keyword for instance variables to avoid conflicting from the parameter variables as shown below:

  class Box( )
  {
        double width;
        double height;
        double depth;
    
        Box(double width, double height, double depth)
        {
            this.width = width;      // we've added 'this' keyword along with dot(.) operator for instance variables
            this.height = height   // Now the variable with the same name on the right will be identified  by Java as parameter variable of Box( ) constructor
            this.depth = depth;
        }
  }

Lets implement this on Eclipse IDE and find out whether it works or not

1. Launch Eclipse IDE, Create a new Java Project 'Project 002', Create a Box Class and Declare instance variables width, height and depth in Box( ) class as shown below:



2. Create Box( ) constructor with the parameter variables names same as instance variable names as shown below:



3. Assign values passed to the Parameter variables to the instance variables of Box class by adding 'this.' before the instance variables of Box class as shown below:


4. Create a volume( ) method which calculates the volume of box using the instance variables of Box class as shown below and Save:



In volume( ) method, observe that we've used instance variables without adding 'this' keyword as the scope of the parameter variables is limited to Box( ) constructor. Hence we're free to use instance variables of Box class as there wont be any conflicts.

5. Create a class named 'ThisDemo' under the Project in which 'Box' class is created as shown below:



6. Create box1 and box2 objects as instances of Box class, by passing the arguments of box1 and box2 during the objects creation itself as shown below:



7. Call the volume( ) method in Box class, retrieve the volumes of box1 and box2 and print them as shown below:



8. Save and Run 'ThisDemo' class as Java Application

9. Observe that the output is displayed in the console of Eclipse IDE as shown below:



Download this project:

Click here to download the project containing 'Box' and 'ThisDemo' classes used in this post. (You can download this project and import into Eclipse IDE on your machine)




Please comment below to feedback or ask questions.

'Method Overloading' concept will be explained in next post.



3 comments:

Minty said...

What is the difference between this and super keyword?

Minty said...

What is the difference between this and super keyword?

Unknown said...

Hii minty,

For this keyword above example is perfect.

Super keyword : Super is a reference variable which is used to call the super class member from sub class

please go through the below example


Example : class parent{
int Main=10; // parent class
int Maine=20;
}
public class Child extends parent{

int Main=30; // child class
int Maine=40;

public void method(int Main,int Maine) {

System.out.println(Main+Maine);

System.out.println(this.Main+this.Maine);

System.out.println(super.Main+super.Maine); }

public static void main(String args[]){

new Child().method(50,1000);

}}

Output : 1050 // local variables with in the method
70 // variables with in the class (this)
30 // variables from parent class (super)