Java This Keywords
Java This Keywords
1
Object Oriented Programming & Java
by
Mr. Santanu Basak
Assistant Professor, Department of Computer Science & Engineering,
University of Engineering & Management, Jaipur
Java this Keyword
● In Java, this refers to the current object inside methods or constructors.
1. class MyClass {
2. int instVar;
3.
4. MyClass(int instVar){
5. this.instVar = instVar;
6. System.out.println("this reference = " + this);
7. }
8. public static void main(String[] args) {
9. MyClass obj = new MyClass(8);
10. System.out.println("object reference = " + obj);
11. }
12. }
● The output will be:
● Notice that the object id of obj and this is the same. Meaning, this is nothing
but the reference to the current object.
Java this Keyword
● In Java, it is not allowed to declare two or more variables having the same
name inside a scope (class scope or method scope). However, instance
variables and parameters may have the same name.
1. class MyClass {
2. int instVar; // instVar instance variable
3.
4. MyClass(int instVar){ // instVar parameter
5. instVar = instVar;
6. }
7. }
● In the above program, the Java compiler is confused due to name ambiguity.
Hence, to solve the problem, this keyword is used.
Java this Keyword
● An example without using this keyword:
1. class MyClass {
2.
3. int instVar;
4. MyClass(int instVar){
5. instVar = instVar;
6. }
7.
8. public static void main(String[] args) {
9. MyClass mc = new MyClass(8);
10. System.out.println("mc.instVar = " + mc.instVar);
11. }
12. }
● The output will be:
mc.instVar = 0
● You might have expected 8 as an output, but, instead you get 0. This is
because the Java compiler gets confused because of the ambiguity in names
between the instance variable and the constructor parameter.
Java this Keyword
● Use this keyword to solve this issue.
1. class MyClass{
2. int instVar;
3. MyClass(int instVar){
4. this.instVar = instVar;
5. }
6. public static void main(String[] args) {
7. MyClass obj = new MyClass(8);
8. System.out.println("obj.instVar = " + obj.instVar);
9. }
10. }
● The output will be:
mc.instVar = 8
● Now, you’ll get the expected output. It’s because when you are creating an
object, the Java compiler knows which object has invoked the constructor.
● When Java compiler invokes the constructor, this inside the constructor is
replaced by the object which is called the constructor.
● If you pass a parameter that has a different name than that of instance
variables, the compiler automatically appends this keyword.
Java this Keyword
1. Program without using this:
class MyClass {
int instVar;
MyClass(int i) {
instVar = i;
}
}
class MyClass {
int instVar;
MyClass(int i) {
this.instVar = i;
}
}
Java this Keyword
● Using setters and getters methods
1. class Employee {
2. String name;
3.
4. void setName( String name ) {
5. this.name = name;
6. }
7.
8. String getName(){
9. return this.name;
10. }
11.
12. public static void main( String[] args ) {
13. Employee emp = new Employee();
14. emp.setName("Ram");
15. System.out.println(emp.getName());
16. }
}
Ram
Java this Keyword
● Using this in Constructor Overloading
○ While working with constructor overloading, you may
find it useful to invoke one constructor from another
constructor. But, constructors can not be called
explicitly. Hence, to accomplish this, you can use
another form of this keyword this().
○ The pseudocode is shown here:
■ this(arg-list)
Java this Keyword
18. public static void main( String[] args ) {
● Call a constructor from another
19. Complex c1 = new Complex(2, 3);
constructor using this. 20. Complex c2 = new Complex(3);
1. class Complex { 21. Complex c3 = new Complex();
2. private int a, b; 22. System.out.println(c1);
3. // parameterize constructor 23. System.out.println(c2);
4. private Complex( int i, int j ){ 24. System.out.println(c3);
5. this.a = i; 25. }
6. this.b = j; 26. }
7. }
8. private Complex(int i){
9. this(i, i); // invokes Complex(int i, int j);
10. } ● The output will be:
11. private Complex(){
12. this(0); // invokes Complex(int i); 2 + 3i
13. } 3 + 3i
14. @Override 0 + 0i
15. public String toString(){
16. return this.a + " + " + this.b + "i"; In the above program, no matter
17. } which constructor is called during the
object instantiation, the parameterized
constructor will be called eventually.
Java this Keyword
● You must be careful while using this(). Constructors that
call this() executes slow because calling another
overloaded constructor adds overhead. If your class is
used to create only a handful of objects, then using this()
is fruitful. Another huge advantage of using this() is to
reduce the amount of duplicate code.
● Invoking one constructor from another constructor is
called explicit constructor invocation.
Java this Keyword
● Passing this as an Argument ● The output will be:
1. class MyClass {
2. int x; Before passing this to addTwo() method:
3. int y; x = 1, y = -2
4. MyClass(int x, int y) { After passing this to addTwo() method:
5. this.x = x; x = 3, y = 0
6. this.y = y;
7. System.out.println("Before passing this to addTwo() method:");
8. System.out.println("x = " + this.x + ", y = " + this.y);
9. addTwo(this);
10. System.out.println("After passing this to addTwo() method:");
11. System.out.println("x = " + this.x + ", y = " + this.y);
12. }
13. void addTwo(MyClass o){
14. o.x += 2;
15. o.y += 2;
● Here, addTwo() method is called
16. } with this as an argument from
17. } inside the constructor.
18. class Demo {
19. public static void main( String[] args ) {
20. MyClass obj = new MyClass(1, -2);
21. }
22. }
UNIVERSITY OF ENGINEERING & MANAGEMENT, JAIPUR
Reference/s
Java
The Complete Reference
by
Herbert Schildt
UNIVERSITY OF ENGINEERING & MANAGEMENT, JAIPUR
Thank
You