[go: up one dir, main page]

0% found this document useful (0 votes)
8 views6 pages

4 This Keyword

The document provides an overview of the 'this' keyword in Java, explaining its role as a reference to the current object within methods and constructors. It outlines six primary usages of 'this', including referring to instance variables and invoking other constructors. Additionally, it includes examples demonstrating variable hiding and constructor invocation using 'this'.

Uploaded by

asim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

4 This Keyword

The document provides an overview of the 'this' keyword in Java, explaining its role as a reference to the current object within methods and constructors. It outlines six primary usages of 'this', including referring to instance variables and invoking other constructors. Additionally, it includes examples demonstrating variable hiding and constructor invocation using 'this'.

Uploaded by

asim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Object Oriented Programming

Lecture Handout-4

Instructor . ASIM Majeed

this keyword in Java


What is this
this is a keyword in Java. It can be used inside the method or constructor of a
class. It(this) works as a reference to the current object, whose method or
constructor is being invoked. This keyword can be used to refer to any
member of the current object from within an instance method or a constructor.

There can be a lot of usage of java this keyword. In java, this is a reference
variable that refers to the current object.

Usage of java this keyword


Here is given the 6 usage of java this keyword.

1. this can be used to refer current class instance variable.


2. this can be used to invoke current class method (implicitly)
3. this() can be used to invoke current class constructor.
4. this can be passed as an argument in the method call.
5. this can be passed as argument in the constructor call.
6. this can be used to return the current class instance from the method.

Suggestion: If you are beginner to java, lookup only three usage of this keyword.
this keyword with a field(Instance Variable)
this keyword can be very useful in the handling of Variable Hiding. We can not
create two instances/local variables with the same name. However, it is legal
to create one instance variable & one local variable or Method parameter with
the same name. In this scenario, the local variable will hide the instance
variable this is called Variable Hiding.

Example of Variable Hiding

class JBT {
int variable = 5;

public static void main(String args[]) {


JBT obj = new JBT();

obj.method(20);
obj.method();
}

void method(int variable) {


variable = 10;
System.out.println("Value of variable :" +
variable);
}

void method() {
int variable = 40;
System.out.println("Value of variable :" +
variable);
}
}
COPY

The output of the above programme

Value of variable :10


Value of variable :40
COPY

As you can see in the example above, the instance variable is hiding, and the
value of the local variable (or Method Parameter) is displayed not instance
variable. To solve this problem use this keyword with a field to point to the
instance variable instead of the local variable.

Example of this keyword in Java for Variable Hiding

class JBT {
int variable = 5;

public static void main(String args[]) {


JBT obj = new JBT();

obj.method(20);
obj.method();
}

void method(int variable) {


variable = 10;
System.out.println("Value of Instance variable :" +
this.variable);
System.out.println("Value of Local variable :" +
variable);
}

void method() {
int variable = 40;
System.out.println("Value of Instance variable :" +
this.variable);
System.out.println("Value of Local variable :" +
variable);
}
}
COPY

The output of the above program

Value of Instance variable :5


Value of Local variable :10
Value of Instance variable :5
Value of Local variable :40
COPY

this Keyword with Constructor


“this” keyword can be used inside the constructor to call another overloaded
constructor in the same Class. It is called the Explicit Constructor Invocation.
This occurs if a Class has two overloaded constructors, one without argument
and another with the argument. Then “this” keyword can be used to call the
constructor with an argument from the constructor without argument. This is
required as the constructor cannot be called explicitly.

Example of this with Constructor

class JBT {

JBT() {
this("JBT");
System.out.println("Inside Constructor without
parameter");
}

JBT(String str) {
System.out
.println("Inside Constructor with
String parameter as " + str);
}

public static void main(String[] args) {


JBT obj = new JBT();
}
}
COPY

The output of the above program

Inside Constructor with String parameter as JBT


Inside Constructor without parameter
COPY

As you can see “this” can be used to invoke an overloaded constructor in the
same Class.

Note*:
● this keyword can only be the first statement in Constructor.
● A constructor can have either this or super keyword but not both.

this Keyword with Method


this keyword can also be used inside Methods to call another Method from
same Class.

Example of this keyword with Method

class JBT {

public static void main(String[] args) {


JBT obj = new JBT();
obj.methodTwo();
}
void methodOne(){
System.out.println("Inside Method ONE");
}

void methodTwo(){
System.out.println("Inside Method TWO");
this.methodOne();// same as calling methodOne()
}
}

Video Lecture Link.


Watch the video at following link.
https://www.youtube.com/watch?v=W7f2vYcVhg0&t=2s

You might also like