Uses of java this keyword

Java this keyword has several uses.this can be used to refer to a current object. Uses 1.Can be used to refer to current va...

Java this keyword has several uses.this can be used to refer to a current object.

Uses

1.Can be used to refer to current variable.
2.can be used to call current class method.
3.Can invoke current class constructor.
4.Can passed in argument method call.
5. Can be passed as argument when calling constructor.
6.Can be used to return current class instance method.

Look at below image that summarizes this keyword uses.
1.Using this to refer to current class variable
Example without this keyword.

/**
 *
 * @author Eric
 */
public class Students {
    
  int id;
String name;
float balance;
 Students(int id,String name,float balance)
{
id=id;
name=name;
balance=balance;
}
   void show()
   {
   System.out.println("ID Number is" +id);
    System.out.println("Name is" +name);
     System.out.println("Balance is" +balance);
   } 
    
   public static void main(String [] args)
   {
   Students s=new Students(123,"Eric mutua",12300f);
    Students s2=new Students(123,"Eric mutua",12300f);
    s.show();
    s2.show();
   }
}

Output:

Same Example with this keyword.


public class Students {
    
  int id;
String name;
float balance;
 Students(int id,String name,float balance)
{
this.id=id;
this.name=name;
this.balance=balance;
}
   void show()
   {
   System.out.println("ID Number is" +id);
    System.out.println("Name is" +name);
     System.out.println("Balance is" +balance);
   } 
    
   public static void main(String [] args)
   {
   Students s=new Students(123,"Eric mutua",12300f);
    Students s2=new Students(124,"Ken",12311f);
    s.show();
    s2.show();
   }
}


Output
Problem where this keyword is not required.


public class Students {
    
  int id;
String name;
float balance;
 Students(int idno,String n,float bal)
{
id=idno;
name=n;
balance=bal;
}
   void show()
   {
   System.out.println("ID Number is" +id);
    System.out.println("Name is" +name);
     System.out.println("Balance is" +balance);
   } 
    
   public static void main(String [] args)
   {
   Students s=new Students(123,"Eric mutua",12300f);
    Students s2=new Students(124,"Ken",12311f);
    s.show();
    s2.show();
   }
}

2.Invoking current class method using this keyword.


public class Students {
    
  int id;
String name;
float balance;
 Students(int idno,String n,float bal)
{
id=idno;
name=n;
balance=bal;
}
   void show()
   {
   System.out.println("ID Number is" +id);
    System.out.println("Name is" +name);
     System.out.println("Balance is" +balance);
   } 
   void call()
   {
       //this method calls method show using this keyword
   this.show();
   }
    
   public static void main(String [] args)
   {
   Students s=new Students(123,"Eric mutua",12300f);
    Students s2=new Students(124,"Ken",12311f);
   s.call();;
   s2.call();
   }
}


3.Invoking current class constructor

Calling default constructor from parameterized constructor:

public class Student {
    Student()
    {
    
    System.out.println("Hello Student");
    }
    Student(int id)
    {
        this();
    System.out.println("Id" +id);
    }
}
    class Test
    {
   public static void main(String args[]){  
    Student s=new Student(124);
    }
    
    }
    



Output:
run:
Hello Student
Id124

BUILD SUCCESSFUL (total time: 0 seconds)

Calling parameterized constructor from default constructor:



public class Student {
    Student()
    {
         this(45);
    
    System.out.println("Hello Student");
    }
    Student(int id)
    {
       
    System.out.println("Id" +id);
    }
}
    class Test
    {
   public static void main(String args[]){  
    Student s=new Student();
    }
    
    }
    


Output
run:
Id45
Hello Student

BUILD SUCCESSFUL (total time: 0 seconds)

4.using this keyword to pass an argument to a method


public class Student {
    void show(Student s)
    {
          System.out.println("method is invoked");  
        
    }
    void invoke()
    {
    show(this);
    }
    
   public static void main(String args[]){  
    Student s=new Student();
    s.invoke();
    
    }
}
    

Output

method is invoked
BUILD SUCCESSFUL (total time: 0 seconds)


5.Using this keyword to pass an argument to a constructor.


class BB{  
  AA4 obj;  
  BB(AA4 obj){  
    this.obj=obj;  
  }  
  void display(){  
    System.out.println(obj.data);//using data member of AA4 class  
  }  
}  
  
class AA4{  
  int data=10;  
  AA4(){  
   BB b=new BB(this);  
   b.display();  
  }  
  public static void main(String args[]){  
   AA4 a=new AA4();  
  }  
}

6.using this keyword to return current class interface


class AA{  
AA getAA(){  
return this;  
}  
void msg(){System.out.println("Hello java");}  
}  
class Test1{  
public static void main(String args[]){  
new AA().getAA().msg();  
}  
}


See also:

The use of Java static keyword


COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: Uses of java this keyword
Uses of java this keyword
https://2.bp.blogspot.com/-nbtjwimVYUs/WWksDa39-SI/AAAAAAAAIR8/eI-N8A3opX8nh5EoyVB2KysFvZvUKfz2ACLcBGAs/s320/uses-of-java-this%2Bkeyword-min.PNG
https://2.bp.blogspot.com/-nbtjwimVYUs/WWksDa39-SI/AAAAAAAAIR8/eI-N8A3opX8nh5EoyVB2KysFvZvUKfz2ACLcBGAs/s72-c/uses-of-java-this%2Bkeyword-min.PNG
Java Codes
http://www.download-all.net/2017/07/uses-of-java-this-keyword.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/07/uses-of-java-this-keyword.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy