What is 'this'?
this is a keyword in Java. It can be used inside the Method or constructor of Class.
this works as a reference to the current Object whose Method or constructor is being invoked.
The this keyword can be used to refer to any member of the current object from within an instance Method or a constructor.
How to use this keyword with field?
this keyword can be very useful in the handling of Variable Hiding.
Example
class Demo { int variable = 5; void method() { int variable = 40; System.out.println("Value of Instance variable :" + this.variable); System.out.println("Value of Local variable :" + variable); } public static void main(String args[]) { Demo obj = new Demo(); obj.method(); } }Try it Yourself
How to use this Keyword with Constructor?
“this” keyword can be used inside the constructor to call another overloaded constructor in the same Class.
This is called the Explicit Constructor Invocation.
This occurs if a Class has overloaded constructors.
Example
class Demo { Demo() { this("Java"); System.out.println("Inside Constructor without parameter"); } Demo(String str) { System.out.println("Inside Constructor with String parameter as " + str); } public static void main(String[] args) { Demo obj = new Demo(); } }Try it Yourself
Note:
this keyword can only be the first statement in Constructor.
A constructor can have either this or super keyword but not both.
How to use this Keyword with Method?
this keyword can also be used inside Methods to call another Method from same Class.
Example
class Demo { void methodOne(){ System.out.println("Inside Method ONE"); } void methodTwo(){ System.out.println("Inside Method TWO"); this.methodOne(); } public static void main(String[] args) { Demo obj = new Demo(); obj.methodTwo(); } }Try it Yourself
How to use this keyword as Method parameter?
Example
class Demo { int i; void method() { method1(this); } void method1(Demo t) { System.out.println(t.i); } } class Demomain { public static void main(String[] args) { Demo obj = new Demo(); obj.i = 10; obj.method(); } }Try it Yourself
Solve this!!
public class ThisDemo { int a; int b; ThisDemo(int a, int b){ this.a=a; this.b=b; } public static void main(String[] args) { ThisDemo obj = new ThisDemo(10, 20); System.out.println(obj.a); System.out.println(obj.b); } }
public class ThisDemo { int a; int b; ThisDemo(int a, int b){ this.a=a; this.b=b; } public static void main(String[] args) { ThisDemo obj = new ThisDemo(); System.out.println(obj.a); System.out.println(obj.b); } }
Error:The constructor ThisDemo() is undefined.
- Collection
- ArrayList
- LinkedList
- Vector
- HashSet
- Conditional Statements
- Classes and Objects
- Inheritance
- Packages
- Exception Handling
- Threads
- TreeSet
- LinkedHashSet
- HashMap
- TreeMap
- LinkedHashMap
- HashTable
- Iterator and ListIterator
- String
- Scanner
- Array Small Programs
- Introduction
- Variables
- DataTypes
- Operators
- Conditional Statements
- Loop
- Arrays
- Object Oriented Programming
- Classes and Objects
- Access Modifiers
- Methods
- Abstraction
- Constructor
- Packages
- StringBuffer
- Inheritance
- StringBuilder
- Encapsulation
- Java IO
- Polymorphism
- Interface
- Types Of Variables
- Static Keyword
- Final Keyword
- Super Keyword
- This Keyword
- Exception Handling