This usage in java
- Introduction to Java
- The complete History of Java Programming Language
- C++ vs Java vs Python
- How to Download and Install Java for 64 bit machine?
- Setting up the environment in Java
- How to Download and Install Eclipse on Windows?
- JDK in Java
- How JVM Works – JVM Architecture?
- Differences between JDK, JRE and JVM
- Just In Time Compiler
- Difference between JIT and JVM in Java
- Difference between Byte Code and Machine Code
- How is Java platform independent?
- Decision Making in Java (if, if-else, switch, break, continue, jump)
- Java if statement with Examples
- Java if-else
- Java if-else-if ladder with Examples
- Loops in Java
- For Loop in Java
- Java while loop with Examples
- Java do-while loop with Examples
- For-each loop in Java
- Continue Statement in Java
- Break statement in Java
- Usage of Break keyword in Java
- return keyword in Java
- Object Oriented Programming (OOPs) Concept in Java
- Why Java is not a purely Object-Oriented Language?
- Classes and Objects in Java
- Naming Conventions in Java
- Java Methods
- Access Modifiers in Java
- Java Constructors
- Four Main Object Oriented Programming Concepts of Java
- Inheritance in Java
- Abstraction in Java
- Encapsulation in Java
- Polymorphism in Java
- Interfaces in Java
- ‘this’ reference in Java
Using the this Keyword
Within an instance method or a constructor, this 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 .
Using this with a Field
The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter.
For example, the Point class was written like this
but it could have been written like this:
Each argument to the constructor shadows one of the object’s fields inside the constructor x is a local copy of the constructor’s first argument. To refer to the Point field x , the constructor must use this.x .
Using this with a Constructor
From within a constructor, you can also use the this keyword to call another constructor in the same class. Doing so is called an explicit constructor invocation. Here’s another Rectangle class, with a different implementation from the one in the Objects section.
public class Rectangle < private int x, y; private int width, height; public Rectangle() < this(0, 0, 1, 1); > public Rectangle(int width, int height) < this(0, 0, width, height); > public Rectangle(int x, int y, int width, int height) < this.x = x; this.y = y; this.width = width; this.height = height; >. >
This class contains a set of constructors. Each constructor initializes some or all of the rectangle’s member variables. The constructors provide a default value for any member variable whose initial value is not provided by an argument. For example, the no-argument constructor creates a 1×1 Rectangle at coordinates 0,0. The two-argument constructor calls the four-argument constructor, passing in the width and height but always using the 0,0 coordinates. As before, the compiler determines which constructor to call, based on the number and the type of arguments.
If present, the invocation of another constructor must be the first line in the constructor.
Ключевое слово this
Click here if the video is not accessible
Understand ‘this’ keyword with an example.
- Class: class Account
- Instance Variable: a and b
- Method Set data: To set the value for a and b.
- Method Show data: To display the values for a and b.
- Main method: where we create an object for Account class and call methods set data and show data.
Let’s compile and run the code
Our expected output for A and B should be initialized to the values 2 and 3 respectively.
But the value is 0, Why? Let investigate.
In the method Set data, the arguments are declared as a and b, while the instance variables are also named as a and b.
During execution, the compiler is confused. Whether “a” on the left side of the assigned operator is the instance variable or the local variable. Hence, it does not set the value of ‘a’ when the method set data is called.
The solution is the “this” keyword
Append both ‘a’ and ‘b’ with the Java this keyword followed by a dot (.) operator.
During code execution when an object calls the method ‘setdata’. The keyword ‘this’ is replaced by the object handler “obj.” (See the image below).
So now the compiler knows,
- The ‘a’ on the left-hand side is an Instance variable.
- Whereas the ‘a’ on right-hand side is a local variable
The variables are initialized correctly, and the expected output is shown.
Suppose you are smart enough to choose different names for your instance variable and methods arguments.
But this time around, you create two objects of the class, each calling the set data method.
How the compiler will determine whether it is supposed to work on instance variable of object 1 or object 2.
Well, the compiler implicitly appends the instance variable with “this” keyword (image below).
Such that when object 1 is calling the set data method, an instance variable is appended by its reference variable.
While object 2 is calling the set data method, an instance variable of object 2 is modified.
This process is taken care by the compiler itself. You don’t have to append ‘this’ keyword explicitly unless there is an exceptional situation as in our example.
Example: To learn the use “this” keyword
Step 1) Copy the following code into a notepad.