Thursday, August 29, 2013

this keyword in Java

The this reference always points to an object’s own instance. Any object can use the this reference to refer to its own instance. Think of the words me, myself, and I: anyone using those words is always referring to themselves. In this post, we'll look into the use of 'this' keyword and it uses in java with example.


What is a class instance
Class instance is an object of a class that you create mostly with the help of 'new' keyword. For instance,
MyClass object = new MyClass();


this Keyword
Within an instance method or a constructorthis is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.

Use of this keyword
  1. It clarify that you are talking about a field, when there is another variable of the same name. Some use this as a means to differentiate instance variables from local variables or method parameters.
  2. Refer to current object as stated above
  3. Invoke other constructor of the current class in your parameterized constructor.
  4. It can be used to return the instance of a class
  5. this can also be used to refer to the outer object
Note: One point to remember is that this is associated with the instance of the class, so it will not work in static methods

USING THIS TO ACCESS VARIABLES AND METHODS
You can use the keyword this to refer to all methods and variables that are accessible to a class. For example, here’s a modified definition of the class Employee: 
The variable name can be accessed in the class Programmer (which extends the class Employee) as follows:
Because there exists an object of class Employee within the class Programmer, the variable name is accessible to an object of Programmer. The variable name can also be accessed in the class Programmer as follows:
The this reference may be used only when code executing within a method block needs to differentiate between an instance variable and its local variable or method parameters. But some developers use the keyword this all over their code, even when it’s not required. Some use this as a means to differentiate instance variables from local variables or method parameters

In the previous example, the class Employee defines an instance variable with the name name. The Employee class constructor also defines a method parameter name, which is effectively a local variable defined within the scope of the method block. Hence, within the scope of the previously defined Employee constructor, there’s a clash of names, and the local variable will take precedence. Using name within the scope of the Employee class constructor block will implicitly refer to that method’s parameter, not the instance variable. In order to refer to the instance variable name from within the scope of the Employee class constructor, you are obliged to use a this reference.

USING THIS TO ACCESS CONSTRUCTORS
You can also differentiate one constructor from another by using the keyword this. Here’s an example in which the class Employee defines two constructors, with the second constructor calling the first one:

To call the default constructor (one that doesn’t accept any method parameters), call this(). Here’s an example:

If present, a call to a constructor from another constructor must be done on the first line of code of the calling constructor.

Above can be seen with the help of following example:

Inside a class method, when a local variable have the same name as one of the instance variable, the local variable shadows the instance variable inside the method block.
Download CodeProgrammer.java

If you know anyone who has started learning java, why not help them out! Just share this post with them. Thanks for studying today!...

1 comment: