SlideShare a Scribd company logo
1 of 3
Download to read offline
Tutorial4us.com
This keyword in Java
This keyword in Java - this keyword used always for represent reference of current object. this
keyword is used at two place at method level and at constructor level.
Using this with a Field
For example, the Area class was written like this
public class Area
{
public int x = 0;
public int y = 0;
//constructor
public Point(int a, int b) {
x = a;
y = b;
}
}
Tutorial4us.com
Using this with a Constructor
public class Area
{
public int x = 0;
public int y = 0;
//constructor
public Area(int x, int y)
{
this.x = x;
this.y = y;
}
}
Important points related to this keyword
 this keyword is used to refer to current object.
 this is always a reference to the object on which method was invoked.
 this can be used to invoke current class constructor.
 this can be passed as an argument to another method.
This keyword Example
class Rectangle
{
int length;
int breadth;
void setDiamentions(int ln,int br)
{
this.length = ln;
this.breadth = br;
Tutorial4us.com
}
}
class RectangleEx
{
public static void main(String args[])
{
Rectangle obj = new Rectangle();
obj.setDiamentions(20,10);
System.out.println("Length of Rectangle : " +obj.length);
System.out.println("Breadth of Rectangle : " + obj.breadth);
}
}
Output :
Length of Rectangle : 20
Breadth of Rectangle : 10
Whenever we define constructor then if the local parameter names and instance variable names are
same then local parameters hide the instance variables (members of class) by overriding it, so to
resolve this ambiguity "this" keyword is used.
class Demo
{
int a,b;
Demo(int a,int b)
{
this.a=a;
this.b=b;
}
}

More Related Content

Viewers also liked

La dignidad y los derechos humanos
La dignidad y los derechos humanosLa dignidad y los derechos humanos
La dignidad y los derechos humanosjjgonzalez12
 
LOS FRANQUICIADORES COMO FUENTE DE FINANCIAMIENTO
LOS FRANQUICIADORES COMO FUENTE DE FINANCIAMIENTOLOS FRANQUICIADORES COMO FUENTE DE FINANCIAMIENTO
LOS FRANQUICIADORES COMO FUENTE DE FINANCIAMIENTOgreciac18
 
CV Nguyen Huu Mai
CV Nguyen Huu MaiCV Nguyen Huu Mai
CV Nguyen Huu MaiMai Nguyen
 
Como trabalhar com clientes difíceis e transformá los em clientes satisfeitos
Como trabalhar com clientes difíceis e transformá los em clientes satisfeitosComo trabalhar com clientes difíceis e transformá los em clientes satisfeitos
Como trabalhar com clientes difíceis e transformá los em clientes satisfeitosAgendor
 
Fractue fatigue and creep
Fractue fatigue and creepFractue fatigue and creep
Fractue fatigue and creepBESSY JOHNY
 
Bozen Summerschool Digital Libraries
Bozen Summerschool Digital LibrariesBozen Summerschool Digital Libraries
Bozen Summerschool Digital LibrariesRené Schneider
 

Viewers also liked (9)

Java adapter
Java adapterJava adapter
Java adapter
 
La dignidad y los derechos humanos
La dignidad y los derechos humanosLa dignidad y los derechos humanos
La dignidad y los derechos humanos
 
ISO Lead Auditor Certificate
ISO Lead Auditor CertificateISO Lead Auditor Certificate
ISO Lead Auditor Certificate
 
LOS FRANQUICIADORES COMO FUENTE DE FINANCIAMIENTO
LOS FRANQUICIADORES COMO FUENTE DE FINANCIAMIENTOLOS FRANQUICIADORES COMO FUENTE DE FINANCIAMIENTO
LOS FRANQUICIADORES COMO FUENTE DE FINANCIAMIENTO
 
CV Nguyen Huu Mai
CV Nguyen Huu MaiCV Nguyen Huu Mai
CV Nguyen Huu Mai
 
SCM
SCMSCM
SCM
 
Como trabalhar com clientes difíceis e transformá los em clientes satisfeitos
Como trabalhar com clientes difíceis e transformá los em clientes satisfeitosComo trabalhar com clientes difíceis e transformá los em clientes satisfeitos
Como trabalhar com clientes difíceis e transformá los em clientes satisfeitos
 
Fractue fatigue and creep
Fractue fatigue and creepFractue fatigue and creep
Fractue fatigue and creep
 
Bozen Summerschool Digital Libraries
Bozen Summerschool Digital LibrariesBozen Summerschool Digital Libraries
Bozen Summerschool Digital Libraries
 

More from Hitesh Kumar

Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++Hitesh Kumar
 
HTML Interview Questions | Basic Html Interview Questions
HTML Interview Questions | Basic Html Interview QuestionsHTML Interview Questions | Basic Html Interview Questions
HTML Interview Questions | Basic Html Interview QuestionsHitesh Kumar
 
Fibonacci Series Program in C++
Fibonacci Series Program in C++Fibonacci Series Program in C++
Fibonacci Series Program in C++Hitesh Kumar
 
CSS Interview Questions for Fresher and Experience
CSS Interview Questions for Fresher and Experience CSS Interview Questions for Fresher and Experience
CSS Interview Questions for Fresher and Experience Hitesh Kumar
 
Factorial Program in C
Factorial Program in CFactorial Program in C
Factorial Program in CHitesh Kumar
 
Prime number program in C
Prime number program in CPrime number program in C
Prime number program in CHitesh Kumar
 
Top JSON Interview Questions for Freshers
Top JSON Interview Questions for FreshersTop JSON Interview Questions for Freshers
Top JSON Interview Questions for FreshersHitesh Kumar
 
Queue in C, Queue Real Life of Example
Queue in C, Queue Real Life of ExampleQueue in C, Queue Real Life of Example
Queue in C, Queue Real Life of ExampleHitesh Kumar
 
Fibonacci series c++
Fibonacci series c++Fibonacci series c++
Fibonacci series c++Hitesh Kumar
 
Encapsulation in C++
Encapsulation in C++Encapsulation in C++
Encapsulation in C++Hitesh Kumar
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in javaHitesh Kumar
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in javaHitesh Kumar
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in javaHitesh Kumar
 
Constructor in java
Constructor in javaConstructor in java
Constructor in javaHitesh Kumar
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in javaHitesh Kumar
 
File handling in C++
File handling in C++File handling in C++
File handling in C++Hitesh Kumar
 
Main method in java
Main method in javaMain method in java
Main method in javaHitesh Kumar
 

More from Hitesh Kumar (20)

Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++
 
HTML Interview Questions | Basic Html Interview Questions
HTML Interview Questions | Basic Html Interview QuestionsHTML Interview Questions | Basic Html Interview Questions
HTML Interview Questions | Basic Html Interview Questions
 
Fibonacci Series Program in C++
Fibonacci Series Program in C++Fibonacci Series Program in C++
Fibonacci Series Program in C++
 
CSS Interview Questions for Fresher and Experience
CSS Interview Questions for Fresher and Experience CSS Interview Questions for Fresher and Experience
CSS Interview Questions for Fresher and Experience
 
Factorial Program in C
Factorial Program in CFactorial Program in C
Factorial Program in C
 
Prime number program in C
Prime number program in CPrime number program in C
Prime number program in C
 
Top JSON Interview Questions for Freshers
Top JSON Interview Questions for FreshersTop JSON Interview Questions for Freshers
Top JSON Interview Questions for Freshers
 
Queue in C, Queue Real Life of Example
Queue in C, Queue Real Life of ExampleQueue in C, Queue Real Life of Example
Queue in C, Queue Real Life of Example
 
Fibonacci series c++
Fibonacci series c++Fibonacci series c++
Fibonacci series c++
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Encapsulation in C++
Encapsulation in C++Encapsulation in C++
Encapsulation in C++
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Ternary operator
Ternary operatorTernary operator
Ternary operator
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
 
Main method in java
Main method in javaMain method in java
Main method in java
 
Features of java
Features of javaFeatures of java
Features of java
 

Recently uploaded

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 

Recently uploaded (20)

Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 

This keyword in java, Why use this keyword in java

  • 1. Tutorial4us.com This keyword in Java This keyword in Java - this keyword used always for represent reference of current object. this keyword is used at two place at method level and at constructor level. Using this with a Field For example, the Area class was written like this public class Area { public int x = 0; public int y = 0; //constructor public Point(int a, int b) { x = a; y = b; } }
  • 2. Tutorial4us.com Using this with a Constructor public class Area { public int x = 0; public int y = 0; //constructor public Area(int x, int y) { this.x = x; this.y = y; } } Important points related to this keyword  this keyword is used to refer to current object.  this is always a reference to the object on which method was invoked.  this can be used to invoke current class constructor.  this can be passed as an argument to another method. This keyword Example class Rectangle { int length; int breadth; void setDiamentions(int ln,int br) { this.length = ln; this.breadth = br;
  • 3. Tutorial4us.com } } class RectangleEx { public static void main(String args[]) { Rectangle obj = new Rectangle(); obj.setDiamentions(20,10); System.out.println("Length of Rectangle : " +obj.length); System.out.println("Breadth of Rectangle : " + obj.breadth); } } Output : Length of Rectangle : 20 Breadth of Rectangle : 10 Whenever we define constructor then if the local parameter names and instance variable names are same then local parameters hide the instance variables (members of class) by overriding it, so to resolve this ambiguity "this" keyword is used. class Demo { int a,b; Demo(int a,int b) { this.a=a; this.b=b; } }