

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is ‘this’ reference in Java?
The this is a keyword in Java which is used as a reference to the object of the current class, with in an instance method or a constructor. Using this you can refer the members of a class such as constructors, variables and methods.
Using “this” you can −
Differentiate the instance variables from local variables if they have same names, within a constructor or a method.
class Student { int age; Student(int age) { this.age = age; } }
Call one type of constructor (parametrized constructor or default) from other in a class. It is known as explicit constructor invocation.
class Student { int age Student() { this(20); } Student(int age) { this.age = age; } }
Example
public class This_Example { // Instance variable num int num = 10; This_Example() { System.out.println("This is an example program on keyword this"); } This_Example(int num) { // Invoking the default constructor this(); // Assigning the local variable num to the instance variable num this.num = num; } public void greet() { System.out.println("Hi Welcome to Tutorialspoint"); } public void print() { // Local variable num int num = 20; // Printing the local variable System.out.println("value of local variable num is : "+num); // Printing the instance variable System.out.println("value of instance variable num is : "+this.num); // Invoking the greet method of a class this.greet(); } public static void main(String[] args) { // Instantiating the class This_Example obj1 = new This_Example(); // Invoking the print method obj1.print(); // Passing a new value to the num variable through parametrized constructor This_Example obj2 = new This_Example(30); // Invoking the print method again obj2.print(); } }
Output
This is an example program on keyword this value of local variable num is : 20 value of instance variable num is : 10 Hi Welcome to Tutorialspoint This is an example program on keyword this value of local variable num is : 20 value of instance variable num is : 30 Hi Welcome to Tutorialspoint
- Related Questions & Answers
- Are ‘this’ and ‘super’ keywords in Java?
- ‘this’ keyword in C#
- What are all the ways keyword ‘this’ can be used in Java?
- 'this' reference in Java
- When should I use the keyword ‘this’ in a Java class?
- What is the benefit of MySQL ‘IS NULL’ and ‘IS NOT NULL’?
- What is the use of ‘ALL’, ‘ANY’, ’SOME’, ’IN’ operators with MySQL subquery?
- Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’ in C++
- What is the significance of ‘^’ in PHP?
- Validate input: replace all ‘a’ with ‘@’ and ‘i’ with ‘!’JavaScript
- Is it possible to assign a reference to this (keyword) in java?
- Replacing ‘public’ with ‘private’ in “main” in Java
- What is the purpose of ‘is’ operator in C#?
- Is it possible to assign a reference to "this" in java?
- What is meant by Java being ‘write once run anywhere’ language?
Advertisements