I have read that in Java you don't have to explicitly bind the this keyword to object, it is done by interpreter. It is opposite to Javascript where you always have to know the value of this. But where is this in Java is pointing - to class or object ? Or does it vary ? This question is a part of my attempt to understand basic OO concepts and design patterns so I can apply them to Javascript. Thank you.
-
1This is quite easily answered using almighty google – keyser May 19 '12 at 15:05
-
possible duplicate of [Java this.method() vs method()](http://stackoverflow.com/questions/6547310/java-this-method-vs-method) – skaffman May 19 '12 at 15:06
-
This tutorial should help you: http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html – ControlAltDel May 19 '12 at 15:03
-
What do you mean by 'explicitly bind to object'? And how is that different in JavaScript? – user207421 May 19 '12 at 17:28
7 Answers
In Java, this
always refers to an object and never to a class.

- 601,492
- 42
- 1,072
- 1,490
in java this is refer Current object
like
public class Employee{
String name,adress;
Employee(){
this.name="employee";
this.address="address";
}
}

- 39,918
- 16
- 117
- 134
this
refers to current object.
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.

- 40,646
- 13
- 77
- 103
The Java language specification states:
When used as a primary expression, the keyword this denotes a value that is a reference to the object for which the instance method was invoked (§15.12), or to the object being constructed.
I.e. it always points to an object, not a class.

- 1,942
- 17
- 17
In java 'this' is a keyword, basically used to refer to the current object. In the following example the setter methods are using 'this' to set the values of name and age of current object.
public class Person {
String name;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static void main(String[] args) {
Person p = new Person();
p.setName("Rishi");
p.setAge(23);
System.out.println(p.getName() + " is " + p.getAge() + " years old");
}
}

- 3,559
- 23
- 34
From the official documentation (found here):
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.
What this means is that inside the code of any class, when you write this
, you specify the fact that you are referring to the current object.
As a side note, you cannot use this
with static fields or methods because they do not belong to any specific object (instance of the class).
-
That's not the 'official documentation', it's a tutorial. The official documentation is the JLS. – user207421 May 19 '12 at 17:29
this keyword is always used in referencing the object of the current class. where as this() is used for the current class constructors. for example:
class circle {
public int radius;
public Circle() {
this.radius = 10; //any default value
}
public Circle(int radius) {
this.radius = radius // here this.radius will set instance variable radius
}
public int areaOfCircle() {
return 3.14*radius*radius;
}
}