Java4teachus

Sunday, June 09, 2019

This Keyword in java

This keyword in Java


➤ This is a keyword which is used in the java programming language.
This is one of reference variable that reffed to current class object. This Keyword is  automatically implicitly  call by jvm during compile time of java program.

 

Purpose Of This keyword In Java?


The main purpose of "This" keyword  is used to differentiate  between the formal parameter in data members of a class. Whenever the formal parameter in data members of a class are simliar then Otherwise we can get the compile time error.

The differentiate between the formal parameters and data members of a class. The data members of a class must be preceded by "This". 

This can be used into 2 ways they are:

  • This. (This Dot)
  • This()  (this off)
This-keyword-In-Java.jpeg


This. (This Dot)
 
This keyword can be used and available in the entire Java program for two purposes.

1. It always points to current class object (it contains reference of current class object).

2. Whenever the formal parameters and Date-Members of class are same then JVM takes ambiguity  problem  ( In order to differentiate between formal parameter and data member of a class must be preceded by "this" Keyword. Otherwise the data member will be treated by "Jvm" as formal parameters.)

Syntax:-

(this. current class data member names)

Without Using Of  THIS Keyword.

Program:-

class Employee
{
int id;
String Name;
float marks;
Employee(int  id String  name, float marks)
{
 id=id;
name=name;
marks=marks;
}
void display()
{
System.out.println("value of id="+id);
System.out.println("value of name="+name);
System.out.println("value of marks="+marks); 
}
} // sub class
class  EmployeeDemo
{
public static void main(String args[])
{
//...Create an object of sub class called Employee... 

Employee e=new Employee("1, lucky,  80.1");
Employee e=new Employee("2, James Gossling,  81.1");
e.display();
e1.dispaly();
}
}

Compling the java Program

Javac EmployeeDemo,java ↵
Java EmployeeDemo 

OutPut:-

0 null values
0 null values

note:- In the above example, formal Parameters and instance data members are same that is why we are using "this" keyword .

Using of This keyword

Program:-
class Employee
{
int id;
String Name;
float marks;
Employee(int  id String  name, float marks)
{
this. id=id;
this. name=name;
this. marks=marks;
}
void display()
{
System.out.println("value of id="+this.id);
System.out.println("value of name="+this.name);
System.out.println("value of marks="+this.marks); 
}
} // sub class 

class  EmployeeDemo
{
public static void main(String args[])
{
//...Create an object of sub class called Employee... 

Employee e=new Employee("1, lucky,  80.1");
Employee e=new Employee("2, James Gossling,  81.1");
e.display();
e1.dispaly();
}
}

OutPut:-

1, lucky, 80.1
2, james Gossling, 81.1


Q. Write a Java program which illustrate of  This Concept..
Program:-

class Test
{
 int a,b;
 Test(int a, int b);
 {
 this.a=a;
 this.b=b;
 this.a=this.a+2;
 this.b=this.b+2;
 a=a+1;
 b=b+1;
 System.out.println("value of a="+this.a);
 System.out.println("value of b="+this.b);
 }
}
class TestDemo
{
 public static void main(string[]argc)
{
 Test t1=new Test (10,20);
 t1.dispaly();
}

No comments:

Post a Comment

Thanks for visiting my site..