3 This Keyword in Java
3 This Keyword in Java
Content Menu ▼
. class Student10{
. int id;
. String name;
http://www.javatpoint.com/thiskeyword 1/9
8/14/2015 this keyword in java javatpoint
.
. Student10(int id,String name){
. id = id;
. name = name;
. }
. void display(){System.out.println(id+" "+name);}
.
. public static void main(String args[]){
. Student10 s1 = new Student10(111,"Karan");
. Student10 s2 = new Student10(321,"Aryan");
. s1.display();
. s2.display();
. }
. }
Test it Now
Output:0 null
0 null
http://www.javatpoint.com/thiskeyword 2/9
8/14/2015 this keyword in java javatpoint
Test it Now
Output111 Karan
222 Aryan
this keyword
. class Student12{
. int id;
. String name;
.
. Student12(int i,String n){
. id = i;
. name = n;
. }
. void display(){System.out.println(id+" "+name);}
. public static void main(String args[]){
. Student12 e1 = new Student12(111,"karan");
. Student12 e2 = new Student12(222,"Aryan");
. e1.display();
. e2.display();
. }
. }
Test it Now
Output:111 Karan
222 Aryan
http://www.javatpoint.com/thiskeyword 3/9
8/14/2015 this keyword in java javatpoint
Test it Now
Output:
default constructor is invoked
default constructor is invoked
111 Karan
222 Aryan
. class Student14{
. int id;
. String name;
. String city;
.
. Student14(int id,String name){
http://www.javatpoint.com/thiskeyword 4/9
8/14/2015 this keyword in java javatpoint
. this.id = id;
. this.name = name;
. }
. Student14(int id,String name,String city){
. this(id,name);//now no need to initialize id and name
. this.city=city;
. }
. void display(){System.out.println(id+" "+name+" "+city);}
.
. public static void main(String args[]){
. Student14 e1 = new Student14(111,"karan");
. Student14 e2 = new Student14(222,"Aryan","delhi");
. e1.display();
. e2.display();
. }
. }
Test it Now
. class Student15{
. int id;
. String name;
. Student15()
{System.out.println("default constructor is invoked");}
.
. Student15(int id,String name){
. id = id;
. name = name;
. this ();//must be the first statement
. }
. void display(){System.out.println(id+" "+name);}
.
. public static void main(String args[]){
. Student15 e1 = new Student15(111,"karan");
. Student15 e2 = new Student15(222,"Aryan");
. e1.display();
. e2.display();
http://www.javatpoint.com/thiskeyword 5/9
8/14/2015 this keyword in java javatpoint
. }
. }
Test it Now
You may invoke the method of the current class by using the this
keyword. If you don't use the this keyword, compiler automatically
adds this keyword while invoking the method. Let's see the example
this keyword
. class S{
. void m(){
. System.out.println("method is invoked");
. }
. void n(){
. this.m();//no need because compiler does it for you.
. }
. void p(){
. n();//complier will add this to invoke n() method as this.n()
. }
. public static void main(String args[]){
. S s1 = new S();
. s1.p();
. }
. }
Test it Now
Output:method is invoked
. class S2{
http://www.javatpoint.com/thiskeyword 6/9
8/14/2015 this keyword in java javatpoint
Test it Now
Output:method is invoked
. class B{
. A4 obj;
. B(A4 obj){
. this.obj=obj;
. }
. void display(){
. System.out.println(obj.data);//using data member of A4 class
. }
. }
.
. class A4{
. int data=10;
. A4(){
. B b=new B(this);
. b.display();
http://www.javatpoint.com/thiskeyword 7/9
8/14/2015 this keyword in java javatpoint
. }
. public static void main(String args[]){
. A4 a=new A4();
. }
. }
Test it Now
Output:10
. return_type method_name(){
. return this;
. }
. class A{
. A getA(){
. return this;
. }
. void msg(){System.out.println("Hello java");}
. }
.
. class Test1{
. public static void main(String args[]){
. new A().getA().msg();
. }
. }
Test it Now
Output:Hello java
http://www.javatpoint.com/thiskeyword 8/9
8/14/2015 this keyword in java javatpoint
Let's prove that this keyword refers to the current class instance
variable. In this program, we are printing the reference variable and
this, output of both variables are same.
. class A5{
. void m(){
. System.out.println(this);//prints same reference ID
. }
.
. public static void main(String args[]){
. A5 obj=new A5();
. System.out.println(obj);//prints the reference ID
.
. obj.m();
. }
. }
Test it Now
Output:A5@22b3ea59
A5@22b3ea59
← prev next →
http://www.javatpoint.com/thiskeyword 9/9