Normally, I use this
in constructors only.
I understand that it is used to identify the parameter variable (by using this.something
), if i
If the instance variables are same as the variables that are declared in the constructor then we use "this" to assign data.
class Example{
int assign;// instance variable
Example(int assign){ // variable inside constructor
this.assign=assign;
}
}
Hope this helps.
Quoting an article at programming.guide:
this
has two uses in a Java program.
The syntax in this case usually looks something like
this.someVariable = someVariable;
This type of use is described here: The 'this' reference (with examples)
The syntax in this case typically looks something like
MyClass() {
this(DEFAULT_VALUE); // delegate to other constructor
}
MyClass(int value) {
// ...
}
This type of use is described here: this(…) constructor call (with examples)
This refers to the object you’re “in” right now. In other words,this refers to the receiving object. You use this to clarify which variable you’re referring to.Java_whitepaper page :37
class Point extends Object
{
public double x;
public double y;
Point()
{
x = 0.0;
y = 0.0;
}
Point(double x, double y)
{
this.x = x;
this.y = y;
}
}
In the above example code this.x/this.y refers to current class that is Point class x and y variables where (double x,double y) are double values passed from different class to assign values to current class .
A quick google search brought this result: http://xahlee.org/java-a-day/this.html
Pretty much the "this" keyword is a reference to the current object (itself).
In Swing its fairly common to write a class that implements ActionListener
and add the current instance (ie 'this') as an ActionListener for components.
public class MyDialog extends JDialog implements ActionListener
{
public MyDialog()
{
JButton myButton = new JButton("Hello");
myButton.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
System.out.println("Hurdy Gurdy!");
}
}
Instance variables are common to every object that you creating. say, there is two instance variables
class ExpThisKeyWord{
int x;
int y;
public void setMyInstanceValues(int a, int b) {
x= a;
y=b;
System.out.println("x is ="+x);
System.out.println("y is ="+y);
}
}
class Demo{
public static void main(String[] args){
ExpThisKeyWord obj1 = new ExpThisKeyWord();
ExpThisKeyWord obj2 = new ExpThisKeyWord();
ExpThisKeyWord obj3 = new ExpThisKeyWord();
obj1.setMyInstanceValues(1, 2);
obj2.setMyInstanceValues(11, 22);
obj3.setMyInstanceValues(111, 222);
}
}
if you noticed above code, we have initiated three objects and three objects are calling SetMyInstanceValues method. How do you think JVM correctly assign the values for every object? there is the trick, JVM will not see this code how it is showed above. instead of that, it will see like below code;
public void setMyInstanceValues(int a, int b) {
this.x= a; //Answer: this keyword denotes the current object that is handled by JVM.
this.y=b;
System.out.println("x is ="+x);
System.out.println("y is ="+y);
}