Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

This keyword in Java programming language

Posted on March 1, 2017September 17, 2019

Table of Contents

  • This keyword in Java programming language
  • This keyword

This keyword in Java programming language

In this tutorial, we will discuss the concept of “This” keyword in Java programming language.

This keyword

 Using this with a field

“This” keyword is used as a reference to the current object that is an instance of the current class.

“This” reference to the current object is useful in situations where a local variable hides or shadows a field with the same name.

 
The base class was written like this
 
public class base
{
public int m=0;
public int n=0;
 
// constructor
public base (int x, int y) 
               {
      m=x;
      n=y;
 
                 }
}
 
 
The base class was written like this
 
public class base
{
public int m=0;
public int n=0;
 
// constructor
public base (int x, int y) 
               {
      this.m=x;
     this. n=y;
 
                 }
}
 
The base class was written like this



Program 1

public class thisis{
int id;
String name;

thisis(int id,String name){
id=id;                                          // not use this keyword
name=name;                             // not use this keyword
}
void display()
{
System.out.println(id+” “+name);
}

public static void main(String args[]){

thisis obj=new thisis(7,”vannan”);
thisis obj1=new thisis(8,”varathan”);
obj.display();
obj1.display();
}

}


output – to int – 0
             – to String – null    

program 2

public class thisis{
int id;
String name;

thisis(int id,String name){   // this is constructor
this.id=id;                            // this keyword index current constructor
this.name=name;
}
void display()
{
System.out.println(id+” “+name);
}

public static void main(String args[]){

thisis obj=new thisis(7,”dffffff”);
thisis obj1=new thisis(8,”dff9888″);
obj.display();
obj1.display();
}

}


Example 3

Nlo need the this keyword

class School
{
int id;
String name;
School(int id1,String name1)
{
name=name1;
id=id1;
}
void show()
{
System.out.println(id+” “+name);
}

public static void main(String args[])
{
School s1=new School(111,”vannan”);
School s2=new School(222,”varathan”);
s1.show();
s2.show();
}
}

this keyword was used to invoke current class method.

 

Example

class mystudent
{
void showresult()
{
System.out.println(“you got very good pass”);
}

void marks()
{
showresult();
}
void display()
{
showresult();
}

public static void main (String args[])
{
mystudent s=new mystudent();
s.display();
}
}

 

 

 

Related post

Final keyword in Java

Super keyword in Java

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes