The Best Technical Blog Ever

twitterrss

Java- this keyword

In java, the keyword this refers to the current class instance. You can use it from any method or constructor to refer to the current object. In this tutorial we are going to learn about the usage of this keyword.


1. refers current class instance variable:

All of us know, it is impossible to declare two local variables with the same name inside the block in Java. Interestingly, when a local variable has the same name as an instance variable, the local variable hides the instance variable. In that situation, you have to use this keyword to differentiate them. So that, fields could still be accessed even though they are shadowed by the parameters. That means, if there is ambiguity between the instance variable and parameter, this keyword resolves the problem of uncertainty. For example, if the constructor's parameters have the same names as the corresponding fields, then use this to access the instance variables by the same name.


this.fieldname;
Example :
this

this


In this program, this keywords are required to distinguish among the parameters named id, firstname, lastname and address and the instance variables. If parameters and instance variables are different, then there is no need to use this keyword. Because in that case, instance variables are not shadowed by local variables.


2. invoke current class method (implicitly):

You can also invoke the method of the current class by using this keyword. For example, if a class defines a method named display, you can call that method from another method within the same class.


this.display();

Obviously, you can also call the display method without the this keyword. If you don't use the this keyword, compiler automatically adds this keyword while invoking the method. Therefore, in most cases, the keyword this is not required. Let's see the example.


Example :
this


3. invokes current class constructor:

If you have several constructors in the class and want to reuse that constructor in the same class, then use this() to call that constructor. The process of calling one constructor from another constructor in the same class is known as constructor chaining. The this() keyword calls the default constructor. In the same way, parameters with this() keyword invokes the other constructor with the equivalent parameter list within the same class. If a constructor uses this(), it must use it in the first line; otherwise, the compiler will report error message. Below example demonstrates local constructor chaining using this() call.


Example :
this


this


4. passing this as an argument in the method:

You can also pass this keyword as an argument in the method call. Let's see the example.


Example :
this


In this program, the System.out.println() method calls the object's toString method to get a string representation of the object and then prints it to the console. By default, toString prints the name of the class that the object was created from and the object's hash code. If you want the println() method to print something more meaningful, provide a toString method of your own to override the default one for the class.


5. passing this as an argument in the constructor call:

You can also pass this keyword in the constructor as an argument. It is very helpful while using one object in multiple classes.


Example :
this


6. returns current class instance:

This keyword is also used to return the current class instance. For this, return type of the method should be the class type.


Example :
this


Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status