21 June 2023

How this Keyword works In Java - learngreen.net

How this Keyword works In Java?



 

In Java, the "this" keyword refers to the current object of a class. It is used to refer to a class’s contemporary instance variable. It can also additionally be used to call a function constructor of a class from any other constructor of the equal class.


Every object in Java has a reference to itself, which might also be retrieved with the "this" keyword. When you use "this" in a method, it refers to the class’s occasion variable, not the method’s neighborhood variable. This makes it effortless to distinguish between a class’s instance variables and local variables.


Advantages of Using this Keyword:-


Many a time, the parameter surpassed in the constructor have the same name as that of an instance variable already existing in the class. Here, the this key-word helps the programmer in averting the identify conflicts between the two.


The this keyword in Java helps the programmer in making the code greater readable and less complicated to apprehend specifically when working with complicated class hierarchies.


Using "this" to call any other constructor in the same classification can limit code duplication and improve code reusability.


Using "this" to refer to instance variables as a substitute of nearby variables can enhance encapsulation and forestall unintended modifications to the incorrect variable


To conclude, this keyword is a effective device in java for referencing to the modern-day object of a class. It is used to differentiate between a class’s instance variables and nearby variables, as properly as to call some other characteristic constructor of the same class. So, this is an essential keyword in Java programming as it helps in enhancing the code readability, reusability, and encapsulation.

 



 //Example 1
 package Interview;
 public class MyClass {
 public static String message ;
  
 public MyClass ( String message) {
 this.message = message;
  }
  
 public void printmessage() {
 System.out.println(this.message);
  }

 public static void main
 (String[] args) {	
 MyClass obj = new MyClass
 ("Welcome to India");
 obj.printmessage();
 // output-> Welcome
 // to India
	}
  }

 /*In this example we do
 *have a class "MyClass" with
 *a private instance variable
 *"message".Constructor
 *take message variable 
 *and assigns
 *it to instance variable 
 *using this keyword.
 *MyClass also has a 
 *method "printmessage"
 *which prints output using
 *this keyword.
 *In main method we do
 *create object of
 *MyClass and called the
 *"printmessage" method.
 */

 



 //Example 2
 package Interview;
 public class MyClass {
 int a;
 //Constructor with parameter
 public MyClass(int a) {
 //Using this keyword
 this.a=a;
 }
  
 public static void main
 (String[] args) {	
 MyClass obj = new MyClass(8);
 System.out.println (obj.a);
 //output-> 8
 
	  }
  }

 



//Example 3
 package Interview;
 public class MyClass {
 private String sentence;
 class nextClass {
 boolean isnextClass = true;
	}
 public  void setSentence() {
 MyClass.this.sentence = 
  "How are you?";
	}
 public static void main
 (String[] args) {	
 MyClass myObj = new MyClass();
 myObj.setSentence();
 System.out.println
 ("Sentence: " + myObj.sentence);
 // Output->
 // Sentence: How are you?
	  }
  }

 



No comments:

Post a Comment