Skip to main content

Posts

Showing posts with the label core

When to use "this" keyword in Java?

"this" keyword plays an important role in Java to reduce the number of lines code and performance. You can observe the following important uses of "this" keyword. To refer current class instance variables. To invoke current class method. To invoke current class constructor. To return the current class instance. Example 1:  class Employee{ int employeeId; String employeeName; Employee(int employeeId,String employeeName){ this.employeeId= employeeId; this.employeeName= employeeName; } void display(){       System.out.println(employeeId+" "+employeeName); } public static void main(String args[]){                Employee e1 = new Employee (111,"John");                Employee e2 = new Employee (222,"Abraham");                e1.display();           ...