this key phrase
The this key phrase can be utilized to refer present class occasion variable. If there’s ambiguity between the occasion variables and parameters, this key phrase resolves the issue of ambiguity.
Different makes use of of this key phrase embrace :
- It may be used to refer present class occasion variable
- It may be used to invoke or provoke present class constructor
- It may be handed as an argument within the methodology name
- It may be handed as argument within the constructor name
- It may be used to return the present class occasion
It may be used to refer present class occasion variable :
Instance :
1 |
class Scholar{ int rollno; String title; Scholar(int rollno, String title){ this.rollno=rollno; this.title=title; } void show(){System.out.println (rollno+" "+title);} } class ThisDemo{ public static void major(String args[]){ Scholar s1=new Scholar(111,"Raj"); Scholar s2=new Scholar(112,"Sonu"); s1.show(); s2.show(); }} |
—————————————————————————————————————————————-
Output : 100 Raj
101 sonu
—————————————————————————————————————————————-
It may be used to refer present class methodology :
Instance :
public class CurrentMethod {
int i=10;
int j=20;
void add(){
System.out.println(i+j);
}
void present(){
this.add();
System.out.println(“addded”);
}
public static void major(String[] args){
CurrentMethod c = new CurrentMethod ();
c.present();
}
}
—————————————————————————————————————————————-
Output : 30
added
—————————————————————————————————————————————-
It may be used to refer present class constructor :
Instance :
1 |
public class Worker {String title, submit;int empno, wage;Worker(String title,String submit, int wage){this.title=title;this.submit=submit;this.wage=wage;}Worker(String title,String submit, int wage, int empno){this(title, submit, wage);this.empno=empno;}void show(){System.<em>out</em>.println(title+" "+submit+" "+wage+" "+empno);}public static void major (String[] args){Worker e = new Worker("ram","Advisor",10000);Worker e2 = new Worker("Shyam","Supervisor",30000,1011);e.show();e2.show();}}<strong>-----------------------------------------------------------------------------------------------</strong>-----------------------------------------Output : ram Advisor 10000 Shyam Supervisor 30000 1011 |
—————————————————————————————————————————————-
NOTE : name to this should be the primary assertion within the constructor in any other case we’ll get compile time error.
1 |
<strong>Instance :</strong>public class Worker {String title, submit;int empno, wage;Worker(String title,String submit, int wage){this.title=title;this.submit=submit;this.wage=wage;}Worker(String title,String submit, int wage, int empno){this.empno=empno;this(title, submit, wage);// compile time error}void show(){System.<em>out</em>.println(title+" "+submit+" "+wage+" "+empno);}public static void major (String[] args){Worker e = new Worker("ram","Advisor",10000);Worker e2 = new Worker("Shyam","Supervisor",30000,1011);e.show();e2.show();}}<strong>----------------------------------------------------------------------------------------------------------------------------------------</strong> |
Output : compile time error
—————————————————————————————————————————————