SlideShare a Scribd company logo
2
Most read
4
Most read
6
Most read
This Keyword
this keyword refers to the current object in a method or
constructor.
 this can be used to refer current class instance variable.
 this() can be used to invoke current class constructor.
 this can be passed as an argument in the method
call.
 this can be passed as argument in the constructor
call.
Example for this keyword
import java.io.*;
class N
{
int a=30;
void display()
{
int a=40;
System.out.println(a); //output 40
System.out.println(this.a); //output-30
}
public static void main(String args[])
{
N ab=new N();
ab.display();
}
this() constructor call can be used to invoke the current class
constructor.
It is used to reuse the constructor.
this() :
class A{
A()
{
System.out.println(“Welcome");
}
A(int x)
{
this();
System.out.println(x);
}
}
class Ab{
public static void main(String args[])
{
A a=new A(10); //output: Welcome
10
}}
 //Calling default
constructor from one
argument constructor
using this keyword
Calling parameterized constructor from default
constructor:
class B
{
B()
{
this(5);
System.out.println(“Welcome");
}
B(int x)
{
System.out.println(x);
}
}
class TestThis6{
public static void main(String args[])
{
B a=new B();
}}
this: to pass as an argument in the
methodclass S2
{
void m(S2 obj)
{
System.out.println("method is invoked");
}
void p()
{
m(this);
}
public static void main(String args[])
{
S2 s1 = new S2();
s1.p();
}
}

More Related Content

PPTX
C Programming Language Part 9
PDF
Rethink programming: a functional approach
PDF
Class 6 2ciclo
TXT
PPTX
C Programming Language Part 11
PPTX
C Programming Language Part 8
PDF
Recursion concepts by Divya
C Programming Language Part 9
Rethink programming: a functional approach
Class 6 2ciclo
C Programming Language Part 11
C Programming Language Part 8
Recursion concepts by Divya

What's hot (19)

PPTX
Stack using Linked List
PDF
Polymorphism
PPTX
CHAPTER 6
PDF
Stack concepts by Divya
PPTX
C Programming Language Part 7
PDF
Generators
DOC
C tech questions
PDF
Inheritance and polymorphism
DOCX
Top MNC'S Interview questions and answers
PPTX
C Programming Language Step by Step Part 2
PDF
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
DOCX
PDF
Implementing stack
PPTX
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
PDF
6. function
PPTX
function in c
PDF
Print Star pattern in java and print triangle of stars in java
PPTX
C sharp 8
PDF
c programming
Stack using Linked List
Polymorphism
CHAPTER 6
Stack concepts by Divya
C Programming Language Part 7
Generators
C tech questions
Inheritance and polymorphism
Top MNC'S Interview questions and answers
C Programming Language Step by Step Part 2
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Implementing stack
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
6. function
function in c
Print Star pattern in java and print triangle of stars in java
C sharp 8
c programming
Ad

Similar to Java this keyword ppt with example (20)

PPTX
constructer.pptx
PPTX
Lecture 4_Java Method-constructor_imp_keywords
DOCX
Keyword of java
PPTX
java input & output statements
PPTX
Java Programs
PPTX
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
PPT
Inheritance and-polymorphism
PPTX
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
PPTX
Object Oriented Programming Inheritance with case study
PPTX
‫Chapter3 inheritance
PPTX
Inheritance.pptx
PPT
Java class
PPTX
Keywords of java
PDF
OOPs & Inheritance Notes
PPT
Java tutorial for Beginners and Entry Level
PPS
Inheritance chepter 7
DOCX
Static variable
PPTX
Language fundamentals ocjp
PDF
Java Basic day-2
DOCX
Methods in Java
constructer.pptx
Lecture 4_Java Method-constructor_imp_keywords
Keyword of java
java input & output statements
Java Programs
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
Inheritance and-polymorphism
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
Object Oriented Programming Inheritance with case study
‫Chapter3 inheritance
Inheritance.pptx
Java class
Keywords of java
OOPs & Inheritance Notes
Java tutorial for Beginners and Entry Level
Inheritance chepter 7
Static variable
Language fundamentals ocjp
Java Basic day-2
Methods in Java
Ad

Recently uploaded (20)

PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Basic Mud Logging Guide for educational purpose
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
master seminar digital applications in india
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Insiders guide to clinical Medicine.pdf
PDF
Classroom Observation Tools for Teachers
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Basic Mud Logging Guide for educational purpose
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
O5-L3 Freight Transport Ops (International) V1.pdf
master seminar digital applications in india
Module 4: Burden of Disease Tutorial Slides S2 2025
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
VCE English Exam - Section C Student Revision Booklet
Insiders guide to clinical Medicine.pdf
Classroom Observation Tools for Teachers
Final Presentation General Medicine 03-08-2024.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Complications of Minimal Access Surgery at WLH
Abdominal Access Techniques with Prof. Dr. R K Mishra
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPH.pptx obstetrics and gynecology in nursing
human mycosis Human fungal infections are called human mycosis..pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf

Java this keyword ppt with example

  • 1. This Keyword this keyword refers to the current object in a method or constructor.  this can be used to refer current class instance variable.  this() can be used to invoke current class constructor.  this can be passed as an argument in the method call.  this can be passed as argument in the constructor call.
  • 2. Example for this keyword import java.io.*; class N { int a=30; void display() { int a=40; System.out.println(a); //output 40 System.out.println(this.a); //output-30 } public static void main(String args[]) { N ab=new N(); ab.display(); }
  • 3. this() constructor call can be used to invoke the current class constructor. It is used to reuse the constructor.
  • 4. this() : class A{ A() { System.out.println(“Welcome"); } A(int x) { this(); System.out.println(x); } } class Ab{ public static void main(String args[]) { A a=new A(10); //output: Welcome 10 }}  //Calling default constructor from one argument constructor using this keyword
  • 5. Calling parameterized constructor from default constructor: class B { B() { this(5); System.out.println(“Welcome"); } B(int x) { System.out.println(x); } } class TestThis6{ public static void main(String args[]) { B a=new B(); }}
  • 6. this: to pass as an argument in the methodclass S2 { void m(S2 obj) { System.out.println("method is invoked"); } void p() { m(this); } public static void main(String args[]) { S2 s1 = new S2(); s1.p(); } }