Advertisement

Google Ad Slot: content-top

Java This Keyword

~1 min read · Java
On this page

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