Java This Keyword

The this keyword in Java is a reference variable that refers to the current object of a class. It is widely used in object-oriented programming for clarity, especially when dealing with instance variables and methods.

Example
class Human{
private int age;
private String name;
public int getAge(){
return age;
}
public void SetAge(int age, Human obj){
Human obj1=obj; // normal method to set age value
obj1.age=age;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name; // this method to set name value
}
}

public class Main {
public static void main(String[] args) {
Human obj=new Human();
obj.SetAge(20,obj);
obj.setName("Joe");
System.out.println(obj.getName()+" : "+obj.getAge());
}
}

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.