Java Program For this Keyword
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
Program 1
package testcoreproject;
class Employee
{
//Instance variable
private int id;
private String name;
private String dept;
Employee(int id,String name,String dept) // Local variable
{
this.id=id;
this.name=name;
this.dept=dept;
}
void display()
{
System.out.println("ID :"+this.id);
System.out.println("NAME :"+this.name);
System.out.println("Department :"+this.dept);
}
}
public class TestThis
{
public static void main(String[] args)
{
Employee E=new Employee(101, "Vinit Sharma", "CS");
E.display();
}
}Program 2
package testcoreproject;
class Employee1
{
//Instance variable
private int id;
private String name;
private String dept;
public Employee1(int id, String name, String dept) {
this.id = id;
this.name = name;
this.dept = dept;
}
@Override
public String toString() {
return "Employee{" + "id=" + id + ", name=" + name + ", dept=" + dept + '}';
}
}
public class TestThis1
{
public static void main(String[] args)
{
Employee1 E=new Employee1(101, "Vinit Sharma", "CS");
System.out.println(E);
}
}Program 3
package testcoreproject;
class Employee2
{
//Instance variable
private int id;
private String name;
private String dept;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
}
public class TestThis2
{
public static void main(String[] args)
{
Employee2 E2=new Employee2();
E2.setId(501);
E2.setName("Vishal Verma");
E2.setDept("Sales");
System.out.println("ID "+E2.getId());
System.out.println("NAME "+E2.getName());
System.out.println("DEPARTMENT "+E2.getDept());
}
}Program 4
package testcoreproject;
class MyClassThis
{
private void display()
{
System.out.println("Display Method of MyClassThis");
}
void show()
{
System.out.println("Show Method of MyClassThis");
this.display();
}
}
public class TestThisMethod
{
public static void main(String[] args)
{
MyClassThis M=new MyClassThis();
M.show();
}
}Program 5
package testcoreproject;
class MyClass2
{
MyClass2()
{
this(344);
System.out.println("Non Parameter constructor of MyClass");
}
MyClass2(int x)
{
System.out.println("Parameter constructor of MyClass "+x);
}
}
public class TestThisconstructor
{
public static void main(String[] args)
{
// MyClass2 M2=new MyClass2();
}
}
Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

